我需要一个脚本,将返回一个指定的词出现在网页上的次数计数。有没有人知道怎么用PHP做到这一点?代码将如下所示:
<?php
$url="watever.com";
the script here
echo(result);
?>我确实有这一点,只是给出了一个计数,每个单词在网页上出现的次数,但我不太确定如何修改它只有一个单词。
$str = file_get_contents('http://www.example.com/');
print_r(array_count_values(str_word_count(strip_tags(strtolower($str)), 1)));发布于 2013-04-16 08:18:25
尝试使用substr_count
$result = (substr_count(strip_tags($str),"mycoolword"));发布于 2013-04-16 08:17:51
我想你是在找吧。
substr_count -计算子字符串出现的次数
发布于 2013-04-16 08:18:33
了解如何使用preg_match - http://php.net/manual/en/function.preg-match.php
其中一个参数是$matches,如果传递该参数,它将把所有匹配项放入该数组中,因此您可以通过执行计数($matches)来获得计数。
php页面有很好的例子,这里有一个:
<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>https://stackoverflow.com/questions/16026690
复制相似问题