php-regexHow to escape a slash when using regex in PHP?
To escape a slash when using regex in PHP, you can use the preg_quote() function. This function takes a string as an argument and returns a version of the string with all regex special characters escaped with a backslash.
For example:
$string = '$^.+*?|/';
echo preg_quote($string);
Output example
\$\^\.\+\*\?\|\/
The preg_quote() function escapes the following characters: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a boolean value?
- How to use regex in PHP to match any digit?
- How to use PHP regex to match a zip code?
- How to use PHP regex to match URL path?
- How to match a space using PHP regex?
- How to match a plus sign in PHP regex?
- How to convert a PHP regex to JavaScript regex?
- How to use PHP regex to match an IP address?
- How to use PHP regex to match letters, numbers and underscores?
See more codes...