English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The preg_match function is used to perform a regular expression match.
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
Search for a match between subject and the regular expression given by pattern.
Parameter description:
$pattern: The pattern to be searched, in string form.
$subject: The input string.
$matches: If the matches parameter is provided, it will be filled with the search results. $matches[0] will contain the complete text matched by the pattern, $matches[1It will contain the text matched by the first capturing subgroup, and so on.
$flags: The flags can be set to the following flag values:
PREG_OFFSET_CAPTURE: If this flag is passed, an additional string offset (relative to the target string) will be attached to the return of each match. Note: This will change the array filled into the matches parameter, making each element an array with the 0th element being the matched string, followed by1An element is the offset of the matching string in the target string subject.
Offset: Usually, the search starts from the beginning of the target string. The optional parameter offset is used to specify starting the search from an unknown beginning of the target string (in bytes).
Returns the number of matches of the pattern. Its value will be 0 times (no match) or 1 The second one, because preg_match() will stop searching after the first match. preg_match_all() is different from this, as it will search the subject until the end. If an error occurs, preg_match() will return FALSE.
<?php
//The "i" mark after the mode separator indicates an insensitive search.
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "Matching string php.\n";
} else {
echo "No matching string php.\n";
}
?>
The execution result is as follows:
Matching string found php.
<?php
/* The \b mark in the pattern indicates a word boundary, so only the independent word "web" will be matched, not
* Partial content of a word like "webbing" or "cobweb" */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
echo "Matching string found.\n";
} else {
echo "No matching string found.\n";
}
if (preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
echo "Matching string found.\n";
} else {
echo "No matching string found.\n";
}
?>
The execution result is as follows:
Matching string found. No matching string found.
<?php
// Get the host name from the URL
preg_match('@^(?:http://)?([^/]+)@i',
"http://www.oldtoolbag.com/index.html", $matches);
$host = $matches[1];
// Get the last two parts of the host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>
The execution result is as follows:
domain name is: oldtoolbag.com
<?php
$str = 'foobar: 2008';
preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
/* The following examples are in php 5.2.2(pcre 7.0) or works in newer versions, however, for backward compatibility, the above method is recommended. */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
print_r($matches);
?>
The execution result is as follows:
Array ( [0] => foobar: 2008 [name] => foobar [1]]=> foobar [digit] => 2008 [2]]=> 2008 )