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 use PHP regex to match a zip code?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a year?
- How to use PHP regex to match special characters?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match URL path?
- How to use PHP regex to match URL?
- How to use PHP regex to match an XML tag?
See more codes...