PHP preg_match 和 preg_match_all 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4088836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP preg_match and preg_match_all functions
提问by sikas
I would like to know what the preg_match
and preg_match_all
functions do and how to use them.
我想知道preg_match
和preg_match_all
函数的作用以及如何使用它们。
回答by romaninsh
preg_match
stops looking after the first match. preg_match_all
, on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.
preg_match
停止关注第一场比赛。preg_match_all
,另一方面,继续查找,直到完成处理整个字符串。一旦找到匹配项,它就会使用字符串的其余部分来尝试应用另一个匹配项。
回答by Sumit
Both preg_matchand preg_match_allfunctions in PHP use Perl compatible regular expressions.
双方的preg_match和preg_match_allPHP函数使用Perl兼容的正则表达式。
You can watch this series to fully understand Perl compatible regular expressions: https://www.youtube.com/watch?v=GVZOJ1rEnUg&list=PLfdtiltiRHWGRPyPMGuLPWuiWgEI9Kp1w
您可以观看本系列以全面了解 Perl 兼容的正则表达式:https: //www.youtube.com/watch?v=GVZOJ1rEnUg&list=PLfdtiltiRHWGRPyPMGuLPWuiWgEI9Kp1w
preg_match($pattern, $subject, &$matches, $flags, $offset)
preg_match($pattern, $subject, &$matches, $flags, $offset)
The preg_match
function is used to search for a particular $pattern
in a $subject
string and when the pattern is found the first time, it stops searching for it. It outputs matches in the $matches
, where $matches[0]
will contain the text that matched the full pattern, $matches[1]
will have the text that matched the first captured parenthesized sub-pattern, and so on.
该preg_match
功能用于搜索特定$pattern
的$subject
字符串和图案时,首次发现的,它停止寻找它。它在 中输出匹配$matches
,其中$matches[0]
将包含与完整模式匹配的文本,$matches[1]
将具有与第一个捕获的带括号的子模式匹配的文本,依此类推。
Example of preg_match()
示例 preg_match()
<?php
preg_match(
"|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><div align=left>this is a test</div>",
$matches
);
var_dump($matches);
Output:
输出:
array(2) {
[0]=>
string(16) "<b>example: </b>"
[1]=>
string(9) "example: "
}
preg_match_all($pattern, $subject, &$matches, $flags)
preg_match_all($pattern, $subject, &$matches, $flags)
The preg_match_all
function searches for all the matches in a string and outputs them in a multi-dimensional array ($matches
) ordered according to $flags
. When no $flags
value is passed, it orders results so that $matches[0]
is an array of full pattern matches, $matches[1]
is an array of strings matched by the first parenthesized sub-pattern, and so on.
该preg_match_all
函数搜索字符串中的所有匹配项,并将它们输出到$matches
按照 排序的多维数组 ( ) 中$flags
。当没有$flags
传递任何值时,它对结果进行排序,以便$matches[0]
是完整模式匹配$matches[1]
的数组,是与第一个带括号的子模式匹配的字符串数组,依此类推。
Example of preg_match_all()
示例 preg_match_all()
<?php
preg_match_all(
"|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><div align=left>this is a test</div>",
$matches
);
var_dump($matches);
Output:
输出:
array(2) {
[0]=>
array(2) {
[0]=>
string(16) "<b>example: </b>"
[1]=>
string(36) "<div align=left>this is a test</div>"
}
[1]=>
array(2) {
[0]=>
string(9) "example: "
[1]=>
string(14) "this is a test"
}
}
回答by Rebecca
A concrete example:
一个具体的例子:
preg_match("/find[ ]*(me)/", "find me find me", $matches):
$matches = Array(
[0] => find me
[1] => me
)
preg_match_all("/find[ ]*(me)/", "find me find me", $matches):
$matches = Array(
[0] => Array
(
[0] => find me
[1] => find me
)
[1] => Array
(
[0] => me
[1] => me
)
)
preg_grep("/find[ ]*(me)/", ["find me find me", "find me findme"]):
$matches = Array
(
[0] => find me find me
[1] => find me findme
)
回答by meder omuraliev
The PHP manual can help you.
PHP 手册可以帮助您。
Let us know if you have trouble understanding.
如果您在理解上有困难,请告诉我们。