关键字密度和实例php脚本

时间:2015-03-27 00:46:27

标签: php regex

我使用以下脚本但它不起作用,它一直说只有一次关键字的实例

也许它与特殊角色有关?不确定如何解决它

   $word_count = explode(' ', "estudiar inglés estudiar inglés asdasd asd asd sadasd estudiar ingles adas dasd estudiar inglés");
$word_count = count($word_count);
$keyword= "estudiar inglés";

echo $keyword;

$keyword_count = preg_match_all("#{$keyword}#si", $text, $matches);
$keyword_count = count($matches);

$density = $keyword_count / $word_count * 100;

$density = number_format($density, 2) . '%';

              echo $keyword_count;   

1 个答案:

答案 0 :(得分:0)

这项工作:

$text       = "some text asoijw oijd cj isoj some text asiojoasj aisojsaij";

$word_count = explode(' ',$text);
$word_count = count($word_count);
$keyword    = "some text";

$keyword_count = preg_match_all("/$keyword/i", $text, $matches);
$keyword_count = count($matches[0]);

$density = $keyword_count / $word_count * 100;
$density = number_format($density, 2) . '%';

echo "Text: $text<br/>"
    ."Keyword: $keyword<br/><br/>"
    ."Matched $keyword_count time(s)<br/>"
    ."Density: $density";

$ matches [0] :preg_match_all将匹配分成子数组