Home > Backend Development > PHP Problem > What is the usage of php preg match?

What is the usage of php preg match?

coldplay.xixi
Release: 2023-03-06 10:46:02
Original
2189 people have browsed it

php preg match usage is to perform a regular expression match, the syntax is [int preg_match (string $pattern, string $subject [, array &$matches [, int $flags.]]].

What is the usage of php preg match?

preg_match The function is used to perform a regular expression match.

Syntax

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

Search for a match between subject and the regular expression given by pattern.

Parameter description:

  • $pattern: the pattern to be searched, String form.

  • $subject: Input string.

  • $matches: If the parameter matches is provided, it will be filled in for the search The result. $matches[0] will contain the text matched by the full pattern, $matches[1] will contain the text matched by the first capturing subgroup, and so on.

  • $flags: flags can be set to the following flag values:

    PREG_OFFSET_CAPTURE: If this flag is passed, the string offset (relative to the target string) will be returned for each occurrence of a match. Note: This will change the array filled in the matches parameter so that each element becomes a string where the 0th element is the matched string and the 1st element is the offset of the matched 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 the search starting from an unknown point in the target string (unit is byte) .

Return value

Returns the number of matches for pattern. Its value will be 0 times (no match) or 1 time, because preg_match () will stop the search after the first match. Unlike preg_match_all(), it will search the subject until it reaches the end. If an error occurs preg_match() returns FALSE.

Example

1. Search for the text string "php":

<?php
//模式分隔符后的"i"标记这是一个大小写不敏感的搜索
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "查找到匹配的字符串 php。";
} else {
    echo "未发现匹配的字符串 php。";
}
?>
Copy after login

The execution result is as follows:

查找到匹配的字符串 php。
Copy after login

2. Search for the word "word"

<?php
/* 模式中的\b标记一个单词边界,所以只有独立的单词"web"会被匹配,而不会匹配
 * 单词的部分内容比如"webbing" 或 "cobweb" */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    echo "查找到匹配的字符串。\n";
} else {
    echo "未发现匹配的字符串。\n";
}
 
if (preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
    echo "查找到匹配的字符串。\n";
} else {
    echo "未发现匹配的字符串。\n";
}
?>
Copy after login

The execution result is as follows:

查找到匹配的字符串。
未发现匹配的字符串。
Copy after login

3. Get the domain name in the URL

<?php
// 从URL中获取主机名称
preg_match(&#39;@^(?:http://)?([^/]+)@i&#39;,
    "http://www.runoob.com/index.html", $matches);
$host = $matches[1];
 
// 获取主机名称的后面两部分
preg_match(&#39;/[^.]+\.[^.]+$/&#39;, $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>
Copy after login

The execution result is as follows:

domain name is: runoob.com
Copy after login

4. Use named subgroups

<?php
 
$str = &#39;foobar: 2008&#39;;
 
preg_match(&#39;/(?P<name>\w+): (?P<digit>\d+)/&#39;, $str, $matches);
 
/* 下面例子在php 5.2.2(pcre 7.0)或更新版本下工作, 然而, 为了后向兼容, 上面的方式是推荐写法. */
// preg_match(&#39;/(?<name>\w+): (?<digit>\d+)/&#39;, $str, $matches);
 
print_r($matches);
 
?>
Copy after login

The execution results are as follows:

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)
Copy after login

If you want to learn more about programming, please pay attention to the php training column!

The above is the detailed content of What is the usage of php preg match?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template