微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP preg_match查找多次出现

正则表达式在 PHP中使用preg_match查找多次出现的相同字符串的正确语法是什么?

例如,查找以下段落中是否出现以下字符串:

$string = "/brown fox jumped [0-9]/";

$paragraph = "The brown fox jumped 1 time over the fence. The green fox did not. Then the brown fox jumped 2 times over the fence"

if (preg_match($string,$paragraph)) {
echo "match found";
}else {
echo "match NOT found";
}
您想要使用 preg_match_all().以下是代码中的外观.实际函数返回找到的项目数,但$matches数组将保存结果:
<?PHP
$string = "/brown fox jumped [0-9]/";

$paragraph = "The brown fox jumped 1 time over the fence. The green fox did not. Then the brown fox jumped 2 times over the fence";

if (preg_match_all($string,$paragraph,&$matches)) {
  echo count($matches[0]) . " matches found";
}else {
  echo "match NOT found";
}
?>

输出

2 matches found

原文地址:https://www.jb51.cc/php/138470.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐