Pass string in this function.

function clean($string){
   $string = str_replace(' ', '-', $string); // Replaces spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

For more info, check this Remove Special Character - Stackoverflow

Answer from Nana Partykar on Stack Overflow
🌐
W3Schools
w3schools.com β€Ί pHp β€Ί php_string_escape.asp
PHP - Escape Characters
In PHP, an escape character is a backslash \ followed by the character you want to insert.
🌐
PHP
php.net β€Ί manual β€Ί en β€Ί function.htmlspecialchars.php
PHP: htmlspecialchars - Manual
1. Use htmlspecialchars() to filter text input values for html input tags. i.e., echo '<input name=userdata type=text value="'.htmlspecialchars($data).'" />'; 2. Use htmlentities() to filter the same data values for most other kinds of html ...
🌐
SitePoint
sitepoint.com β€Ί php
Correct rules for escaping special characters in a preg_match() - PHP - SitePoint Forums | Web Development & Design Community
July 3, 2022 - I am trying to learn how to use preg_match() and already I am having problems understanding the logic. At the moment I am exploring searching for special characters using escapes. I have established that the special characters that need escaping are . \ + * ? [ ^ ] $ ( ) { } = ! | : - All good so far as in if (preg_match("/\?/", $string)) {... But if I search for a \ then I need to escape it with a \\as in if (preg_match("/\\\/", $string)) {... Ok, I can accept that as a special case but...
🌐
MojoAuth
mojoauth.com β€Ί escaping β€Ί php-string-escaping-in-php
PHP String Escaping in PHP | Escaping Methods in Programming Languages
Here are some common special characters that can be escaped: Single Quote: \' – Used within single-quoted strings. Double Quote: \" – Used within double-quoted strings. Backslash: \ – Escapes the backslash itself.
🌐
TinyMCE
tiny.cloud β€Ί blog β€Ί php-escape
How to work with PHP escape quotes and characters | TinyMCE
December 7, 2023 - It’s a method typically used alongside echo in PHP to automatically add the PHP escape character to a string. ... This function returns a list of each instance of a single character, and allows you to replace that with an escaped character. Useful for finding and replacing multiple special ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί php β€Ί how-to-use-escape-characters-in-php
How to use Escape Characters in PHP ? - GeeksforGeeks
July 12, 2024 - Escape characters behave differently in single-quoted strings compared to double-quoted strings. In single-quoted strings, only the backslash itself (\\) and the single quote (\') need to be escaped. ... PHP offers Heredoc and Nowdoc syntaxes for working with multi-line strings, which can include escape characters for various purposes.
Find elsewhere
🌐
Medium
medium.com β€Ί @ntohtambe β€Ί removing-special-characters-in-php-a41ca6d964d1
Removing Special Characters in PHP | by Tambe Salome | Medium
May 2, 2022 - Moving to PHP from a C background, it seemed more easy to deal with strings, there’s almost a function for anything, even for this. The only problem is knowing that the function exists and understanding how to use it, at least that was my problem. When storing text that contains special characters such as: double quotes, singles quotes, newline character and others, to some databases, escape ...
🌐
FlatCoding
flatcoding.com β€Ί home β€Ί php escape characters: how to escape special characters
PHP Escape Characters: How to Escape Special Characters - FlatCoding
April 25, 2025 - The htmlspecialchars() and htmlentities() help escape special characters before sending the text to a web page. This prevents HTML code or scripts from running, which protects your site from attacks like cross-site scripting (XSS). ...
🌐
SSOJet
ssojet.com β€Ί escaping β€Ί javascript-string-escaping-in-php
JavaScript String Escaping in PHP | Escaping Techniques in Programming
When embedding JavaScript strings within your PHP code, especially for use in HTML or directly in script tags, you must account for characters that hold special meaning. These characters, such as single quotes ('), double quotes ("), and backslashes (), can prematurely terminate your JavaScript string or cause syntax errors if not properly escaped.
🌐
SitePoint
sitepoint.com β€Ί php
PHP escape character in strings - PHP - SitePoint Forums | Web Development & Design Community
August 20, 2014 - I would like to be able to type ... the escape character β€œ\”. What annoys me however is that if I want to do the following: $route = β€œ\{controller\}/{action}/{id}”; PHP automatically converts this to: $route = β€œ{controller}/{action}/{id}”; Despite the { and } symbols having no special meaning ...
🌐
Hacking with PHP
hackingwithphp.com β€Ί 2 β€Ί 6 β€Ί 2 β€Ί escape-sequences
Escape sequences – Hacking with PHP - Practical PHP
You can achieve the same effect in double-quoted strings by using the escape character, which, in PHP, is a backslash \. Escape sequences, the combination of the escape character \ and a letter, are used to signify that the character after the escape character should be treated specially.
🌐
PHPpot
phppot.com β€Ί php β€Ί php-escape-sequences
PHP Escape Sequences: Quotes, Newlines and Tabs - PHPpot
1 month ago - Single-quoted strings are much simpler. PHP normally recognizes only \' for a single quote and \\ for a backslash. A sequence such as \n remains the two literal characters \ and n.
🌐
DigitalOcean
digitalocean.com β€Ί community β€Ί tutorials β€Ί how-to-work-with-strings-in-php
How To Work with Strings in PHP | DigitalOcean
A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. In this article, you will learn how to create and view the…
🌐
phpFashion
phpfashion.com β€Ί en β€Ί escaping-the-definitive-guide
Escaping – The Definitive Guide
May 19, 2009 - In the string replacing the searched ... 1-byte or UTF-8, depending on the modifier in the regular expression. ... Escape is done with the character \. This is usually done by the programmer when writing code, for PHP code generators, ...
🌐
Team Treehouse
teamtreehouse.com β€Ί library β€Ί php-basics-2 β€Ί escape-sequence
Escape Sequence (How To) | PHP Basics | Treehouse
Escape Sequence. Escape sequences are used within strings to denote special meaning. begin with the escaping character backslash (\) and are used ...
Published: November 22, 2019
🌐
MIT App Inventor
community.appinventor.mit.edu β€Ί general discussion
Str_replace or escape of special characters in .php - General Discussion - MIT App Inventor Community
June 14, 2023 - Hi again. The topic this time is str_replace statement used in a .php serving our App to implement a message-like feature through Mysql DB. The issue is that the special character " is not tolerated in the message string, even using str_replace in the .php to replace this character with space or something else causes error and App to crash.