PHP preg_match function

Description

PHP's main pattern-matching function is preg_match().

Syntax

PHP preg_match function has the following syntax.

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

Parameters

ParameterDescription
patternThe pattern to search for, as a string.
subjectThe input string.
matchesIf matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
flagsflags can be the following flag:
offsetNormally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).

Possible value for flags

  • PREG_OFFSET_CAPTURE If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.

Return

preg_match() returns zero if the pattern didn't match, or 1 if it did match.

Note

preg_match() only finds the first match within the string. If you need to find all the matches in a string, use preg_match_all().

Example 1

To match the word "world" anywhere in the string "Hello, world! "


<?PHP
echo preg_match( "/world/", "Hello, world!" );  // Displays "1"   
?>

To match " world " only at the start of the string, you ' d write:


<?PHP
echo preg_match( "/^world/", "Hello, world!" );  // Displays "0"   
?>

To access the text that was matched, pass an array variable as the third argument:


<?PHP
echo preg_match( "/world/", "Hello, world!", $match ) . "\n";  //  Displays "1" 
echo $match[0] . "\n "; // Displays "world"   
?>

To find out the position of the match, pass PREG_OFFSET_CAPTURE as the fourth argument. The array then contains a nested array whose first element is the matched text and whose second element is the position:


<?PHP//  w  ww  . j a v  a 2s.c o m
echo preg_match( "/world/", "Hello, world!", $match, PREG_OFFSET_CAPTURE ) . "\n";  // Displays "1" 
echo $match[0][0] . "\n";                // Displays "world" 
echo $match[0][1] . "\n ";                // Displays "7"   
?>

To start the search from a particular position in the target string, pass the position as the fifth argument:


<?PHP
echo preg_match( "/world/", "Hello, world!", $match, 0, 8 );  // Displays "0"   
?>

Example 2

Using offset is not equivalent to passing substr($subject, $offset) to preg_match() in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). Compare:


<?php//  w ww .  j  a v a2s.c om
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>

Compare:


<?php// w w w. j  av a 2  s .  c om
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

Example 3

The "i" after the pattern delimiter indicates a case-insensitive search.


<?php//from w w  w. j  ava  2s.  c  om

if (preg_match("/php/i", "PHP or php is a scripting language.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

The code above generates the following result.

Example 4

The following code tries to find the word "web".

The \b in the pattern indicates a word boundary, so only the distinct word "web" is matched, and not a word partial like "webbing" or "webber".


<?php/*w  ww .  j  av  a2 s  .c  om*/
if (preg_match("/\bweb\b/i", "The web is huge.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

if (preg_match("/\bweb\b/i", "The website is huge.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

The code above generates the following result.

Example 5

Getting the domain name out of a URL


/*w  ww .ja v  a2  s . com*/
<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',"http://www.php.net/index.html", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}";
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Development »




Environment
Error
Hash
Include
Locale
Math
Network
Output
Reflection
PHP Regular Expressions