php-regexHow to match a question mark in PHP regex?
To match a question mark in PHP regex, use the \?
character. This will match a literal question mark, as opposed to a wildcard character.
Example code
$string = 'This is a string with a question mark?';
$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 match in.$pattern
: This is the regular expression pattern that we are using to search for a match.preg_match()
: This is the PHP function that we are using to search for a match. It takes two parameters: the regular expression pattern and the string to search in.
Helpful links
More of Php Regex
- How to match a space using 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 use PHP regex to match special characters?
- 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 a hashtag?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match URL path?
- How to use named capture groups in PHP regex?
See more codes...