php-regexHow to match a double quote in PHP regex?
To match a double quote in PHP regex, you can use the following code:
$string = 'This is a "string"';
$pattern = '/"(.*?)"/';
preg_match($pattern, $string, $matches);
The output of the above code will be:
Array
(
[0] => "string"
[1] => string
)
Code explanation
$string
: This is the string that we are trying to match.$pattern
: This is the regular expression pattern that we are using to match the double quote. The pattern/"(.*?)"/
will match any double quote that is surrounded by other characters.preg_match()
: This is the PHP function that is used to match the regular expression pattern against the string.
Helpful links
More of Php Regex
- How to use PHP regex to match an exact string?
- How to match a single quote in PHP regex?
- How to get last matched occurrence in PHP regex?
- How to use PHP regex to match a boolean value?
- How to use an "or" condition in PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match UUID?
- How to match a space using PHP regex?
- How to remove all non-numeric characters using PHP regex?
See more codes...