我是一個php中的新手,所以,我正在製作一個字計數器程序。我試圖計算特定單詞在網站中的實例數量。 所以,我使用Substr_count來計算單詞的數量,但問題是它會像「陽光」一樣包含像「陽光」這樣的詞彙。substr_count()在php中計算整個單詞
這是我的代碼。
/*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' - 較小,較小(部分)和字符串......它沒有考慮到什麼「單詞」是 –
@ Wolvy - 你應該考慮使用正則表達式代替 –