Php preg_match查找字符串中单词的多个部分外观
这附近有雷格克斯大师吗?这快把我逼疯了 假设我有这个字符串: “书店书籍预订” 我想数一数这本书中出现的“books”的数量,然后返回数字 目前我有一个不起作用的: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 {
$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_计数,但它似乎对“书店”值不起作用。不知何故,它只对“书”起作用