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 get a YouTube video ID?
- How to use PHP regex to match a zip code?
- How to use PHP regex with zero or more occurrences?
- How to match a plus sign in PHP regex?
- How to use PHP regex to match tab?
- How to remove non-printable characters using PHP regex?
- How to use PHP regex to match a year?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match an exact string?
See more codes...