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 URL path?
- How to use PHP regex to match URL?
- How to use PHP regex to match time?
- How to remove a tag from a string using PHP regex?
- How to replace a tag using PHP regex?
- How to match a single quote in PHP regex?
- How to match a plus sign in PHP regex?
- How to use quantifiers in PHP regex?
- How to use PHP regex to match a multiline?
See more codes...