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."次";

?>

posted @ 2022-06-07 15:46  垖垏尐  阅读(74)  评论(0编辑  收藏  举报