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 with zero or more occurrences?
- How to match a space using PHP regex?
- How to match a single quote in PHP regex?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match special characters?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match a year?
- How to use the "s" modifier in PHP regex?
- How to replace a tag using PHP regex?
See more codes...