Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match查找字符串中单词的多个部分外观_Php_Count_Preg Match_Preg Match All - Fatal编程技术网

Php preg_match查找字符串中单词的多个部分外观

Php preg_match查找字符串中单词的多个部分外观,php,count,preg-match,preg-match-all,Php,Count,Preg Match,Preg Match All,这附近有雷格克斯大师吗?这快把我逼疯了 假设我有这个字符串: “书店书籍预订” 我想数一数这本书中出现的“books”的数量,然后返回数字 目前我有一个不起作用的: $string = "bookstore books Booking"; if (preg_match_all('/\b[A-Z]+books\b/', $string, $matches)) { echo count($matches[0]) . " matches found"; } else {

这附近有雷格克斯大师吗?这快把我逼疯了

假设我有这个字符串: “书店书籍预订”

我想数一数这本书中出现的“books”的数量,然后返回数字

目前我有一个不起作用的:

$string = "bookstore books Booking";            
if (preg_match_all('/\b[A-Z]+books\b/', $string, $matches)) {
  echo count($matches[0]) . " matches found";
} else {
  echo "match NOT found";
}
除此之外,preg_match_中的“书籍”都应该成为$var


有人知道如何正确计数吗?

实际上更简单,您可以这样使用:

$string = "bookstore books Booking";   
$var = "books";      
if (preg_match_all('/' . $var . '/', $string, $matches)) {
    echo count($matches[0]) . " matches found";
} else {
    echo "match NOT found";
}
或者使用为此目的而创建的功能:


实际上更简单,您可以这样使用:

$string = "bookstore books Booking";   
$var = "books";      
if (preg_match_all('/' . $var . '/', $string, $matches)) {
    echo count($matches[0]) . " matches found";
} else {
    echo "match NOT found";
}
或者使用为此目的而创建的功能:


谢谢你的澄清,我测试了t thx,是的,我一直在看subtr_计数,但它似乎对“书店”值不起作用,它只对“书”起作用。谢谢你的澄清,我测试了t thx,是的,我一直在看subtr_计数,但它似乎对“书店”值不起作用。不知何故,它只对“书”起作用