1

这是我的问题,我在 PHP 中有一个文本:

$text = "Car is going with 10 meters/second"

$find = array("meters","meters/second");

现在当我这样做时:

 foreach ($find as $f)
   {
     $count = substr_count($text,$f);
    } 

输出是:

meters -> 1
meters/second -> 1 

通常我认为米/秒是一个完整的词,所以不应该计算米,只有米/秒,因为没有空格分隔它们

因此,我的期望:

meters -> 0
meters/second -> 1
4

3 回答 3

1

您可以使用正则表达式来执行此操作,\b因为/它是单词边界,所以不起作用,但类似的东西应该可以工作:

preg_match_all(",meters([^/]|$),", $text, $matches);
print_r($matches[0]);
于 2013-06-29T21:25:29.023 回答
0
$exists = preg_match("/\bmeters\b/", $text) ;

\b代表单词边界。

于 2013-06-29T21:21:30.500 回答
0

要做你想做的事,你必须使用正则表达式。就像是:

$text = "Car is going with 10 meters/second";
$find = array("/\bmeters\b/", "/\bmeters\/second\b/");

foreach($find as $f) {
    print(preg_match_all($f, $text));
}
于 2013-06-29T21:21:37.703 回答