【发布时间】:2017-03-01 07:28:19
【问题描述】:
我是 php 的新手,所以,我正在制作一个单词计数器程序。我试图计算网站中有多少特定单词的实例。 所以,我使用 Substr_count 来计算单词,但问题是它把像“sunlight”这样的单词当作包含像“sun”这样的单词。
这是我的代码。
/*When the user types the word*/
$search = $_POST["texto"];
/*The website*/
$page = $_POST["Web"];
$web = file_get_contents($page);
/*Count words*/
$result = (substr_count(strip_tags(strtolower($web)), strtolower($search)));
/*Display the information*/
if($result == 0){
echo "the word " .mb_strtoupper($search). " doesn't appear";
}else{
echo "the word " .mb_strtoupper($search). " appears $result times";
}
有什么办法可以解决这个问题吗?我尝试了 str_word_count 和 preg_match_all 但这显示的数字很大。
【问题讨论】:
-
计算机如何知道它只需要选择太阳而不是太阳光?你能在问题中添加一些示例数据吗
-
@Wolvy substr - 完全代表
sub- 较小、较小(部分)和string...它没有考虑“单词”是什么 -
@Wolvy - 你应该考虑改用正则表达式
-
@Wolvy,使用正则表达式。检查这个话题stackoverflow.com/questions/9348326/…