php查找字符串出现几次
方法1:使用substr_count()函数
substr_count() 函数计算子串在字符串中出现的次数(区分大小写的)。
<?php
header("Content-type:text/html;charset=utf-8");
$str="I love Shanghai. Shanghai is the biggest city in china.";
$count=substr_count($str,"Shanghai");
echo "Shanghai 出现了:".$count."次";
?>
方法2:使用mb_substr_count()函数
mb_substr_count()函数统计字符串出现的次数。
<?php
header("Content-type:text/html;charset=utf-8");
$str="我爱上海。上海是中国最大的城市。";
$count=mb_substr_count($str,"上海");
echo "上海 出现了:".$count."次";
?>

浙公网安备 33010602011771号