//以下均以hello作为要查找的单词字符
方法一:
利用file_get_contents()函数获取文件内容,然后对内容以要查找的单词进行explode()拆分,最后将拆分的数组元素个数减去一
代码:
$contents = file_get_contents('a.txt');
$arr = explode('hello',$contents);
$c = count($arr)-1;
echo $c;
方法二:
利用file_get_contents()函数获取文件内容,然后进行全局匹配,最后计算匹配的数组的元素个数
代码:
$contents = file_get_contents('a.txt');
preg_match_all('/hello/',$contents,$matches);
echo count($matches);