php-regexHow to match a single quote in PHP regex?
Single quotes can be matched in PHP regex using the \ character. This is an escape character that allows you to match a single quote in a regex expression.
Example code
$string = 'This is a string with a single quote ' inside';
$pattern = '/\'/';
if (preg_match($pattern, $string)) {
echo 'Match found';
}
Output example
Match found
Code explanation
$string: This is the string that we are searching for a single quote in.$pattern: This is the regex pattern that we are using to search for a single quote. The\character is used to escape the single quote so that it can be matched.preg_match(): This is the PHP function that is used to search for a pattern in a string. It takes two parameters, the regex pattern and the string to search in.
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...