php-regexHow to match a quotation mark in PHP regex?
To match a quotation mark in PHP regex, you can use the \
character before the quotation mark. This is known as an escape character and it tells the regex engine to treat the quotation mark as a literal character instead of a special character.
Example code
$string = 'This is a "test" string';
$pattern = '/".*"/';
if (preg_match($pattern, $string)) {
echo 'Match found!';
}
Output example
Match found!
Code explanation
$string = 'This is a "test" string';
: This is the string that we are searching for a match in.$pattern = '/".*"/';
: This is the regex pattern that we are using to search for a match. The\
character before the quotation mark tells the regex engine to treat the quotation mark as a literal character instead of a special character.if (preg_match($pattern, $string)) {
: This is the condition that will be checked to see if a match is found.echo 'Match found!';
: This is the code that will be executed if a match is found.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to match a space using PHP regex?
- How to use PHP regex to match a year?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match UUID?
- How to use PHP regex to match http or https?
- How to replace strings using PHP regex?
- How to match the end of a string when using regex in PHP?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match special characters?
See more codes...