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.if
statement: This is used to check if the pattern matches the string. If it does, the code inside theif
statement will be executed.
Helpful links
More of Php Regex
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match special characters?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match a word?
- How to use backslash in PHP regex?
- How to use PHP regex to match UUID?
- How to use PHP regex to match UTF8?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match an XML tag?
See more codes...