php-regexHow to use PHP regex to match an exact string?
To match an exact string using PHP regex, you can use the preg_match() function. This function takes two parameters: the pattern to match and the string to search. The pattern should be enclosed in forward slashes (/).
For example, to match the exact string Hello World, you can use the following code:
$string = 'Hello World';
$pattern = '/Hello World/';
if (preg_match($pattern, $string)) {
echo 'Match found!';
}
Output example
Match found!
The code above consists of the following parts:
$string: This is the string to search.$pattern: This is the pattern to match. It should be enclosed in forward slashes (/).preg_match(): This is the function used to match the pattern against the string. It takes two parameters: the pattern and the string.ifstatement: This is used to check if the pattern matches the string. If it does, the code inside theifstatement will be executed.
Helpful links
More of Php Regex
- How to match a quotation mark in PHP regex?
- How to use PHP regex to match a year?
- How to use PHP regex to match URL path?
- How to match a single quote in PHP regex?
- How to match a question mark in PHP regex?
- How to use regex in PHP to match only digits?
- How to use regex in PHP to match any character including newline?
- How to match a double quote in PHP regex?
- How to use PHP regex to get a YouTube video ID?
See more codes...