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 ...
Discussions

How Does base64_decode Return a String Without Escaping Special Characters?
You are confusing a string literal with the actual string value. A string literal is how you write a string in PHP code, i.e. between single/double quotes, with the appropriate escape sequences. The escape sequences are not actually part of the string; they are just the syntax for describing the string. More on reddit.com
๐ŸŒ r/PHP
14
6
November 26, 2014
How do I escape single quotes in PDO query without pdo::quote?
Use bound parameters instead? $stm = $pdo->prepare('SELECT * FROM whatever WHERE some_col LIKE ?'); $like = "Don't%"; $stm->execute([$like]); foreach ($stm as $row) { // ... } More on reddit.com
๐ŸŒ r/PHP
14
0
April 10, 2015
People also ask

What are escape characters in PHP?
Escape characters in PHP are special symbols used inside strings to represent characters that are hard to type or that have special meanings. They prevent PHP from misreading parts of a string.
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php escape characters: how to escape special characters
PHP Escape Characters: How to Escape Special Characters - FlatCoding
Why do we use escape characters in PHP?
We use escape characters to avoid errors when a string includes symbols like quotes, tabs, or backslashes. Without them, PHP might end the string early or misread the input.
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php escape characters: how to escape special characters
PHP Escape Characters: How to Escape Special Characters - FlatCoding
What are the most common escape characters in PHP?
  • \n โ€“ new line
  • \t โ€“ tab space
  • \\ โ€“ backslash
  • \" โ€“ double quote
  • \' โ€“ single quote (only in single-quoted strings)
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php escape characters: how to escape special characters
PHP Escape Characters: How to Escape Special Characters - FlatCoding
๐ŸŒ
PHP.Watch
php.watch โ€บ articles โ€บ php-character-escape-sequences-numeric-notations
Character escape sequences and numeric notations in PHP โ€ข PHP.Watch
February 28, 2021 - Because PHP interprets and interpolates special characters inside double-quoted string literals and heredoc string literals, the backslash sign (\) is used as an "escape character".
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
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). ...
๐ŸŒ
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.
๐ŸŒ
Code Boxx
code-boxx.com โ€บ home โ€บ php escape characters & sequences (simple examples)
PHP Escape Characters & Sequences (Simple Examples)
November 14, 2023 - A quick recap โ€“ Strings can be defined using either single or double quotes. But the problem comes when we actually try to add quotes inside a string or use โ€œreserved charactersโ€ such as dollar and slash. ... <?php // (A) ESCAPE SINGLE QUOTE $strA = 'Foo\'s Bar.'; echo $strA .
๐ŸŒ
BUSoft Technologies
busofttech.com โ€บ home โ€บ php escape quotes how to do it right?
PHP Escape Quotes: How to Do It Right | BUSoftTech
March 10, 2026 - Escape sequences are started with the escaping character backslash (\) followed by the character which may be an alphanumeric or a special character. If it is an alphanumeric character, it gives special meaning to represent the line breaks \n, ...
๐ŸŒ
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...
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ php-cookbook โ€บ 1565926811 โ€บ ch13s09.html
13.8. Escaping Special Characters in a Regular Expression - PHP Cookbook [Book]
November 20, 2002 - You want to have characters such as * or + treated as literals, not as metacharacters, inside a regular expression. This is useful when allowing users to type in search strings you want to use inside a regular expression. Use preg_quote( ) to escape Perl-compatible regular-expression metacharacters:
Authors: David SklarAdam Trachtenberg
Published: 2002
Pages: 640
๐ŸŒ
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, ...
๐ŸŒ
EITCA
eitca.org โ€บ home โ€บ what is the purpose of escaping characters in php strings?
What is the purpose of escaping characters in PHP strings? - EITCA Academy
August 8, 2023 - Escaping characters involves adding a backslash () before the special character to indicate that it should be treated as a literal character rather than having its usual interpretation. One common example is the use of single quotes and double quotes in PHP strings.