9951 explained code solutions for 126 technologies


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:

  1. $string: This is the string to search.
  2. $pattern: This is the pattern to match. It should be enclosed in forward slashes (/).
  3. preg_match(): This is the function used to match the pattern against the string. It takes two parameters: the pattern and the string.
  4. if statement: This is used to check if the pattern matches the string. If it does, the code inside the if statement will be executed.

Helpful links

Edit this code on GitHub