自己用PHP做一个功能和substr_count函数相同的函数 用循环和判断来作 过程中不能出现substr_count
答案:3 悬赏:70 手机版
解决时间 2021-02-28 17:43
- 提问者网友:ミ烙印ゝ
- 2021-02-28 00:14
自己用PHP做一个功能和substr_count函数相同的函数 用循环和判断来作 过程中不能出现substr_count
最佳答案
- 五星知识达人网友:毛毛
- 2021-02-28 01:52
function my_substr_count($str, $sub, $start = 0, $length = FALSE)
{
if(!$length){
$length = strlen($str) - $start;
}
$str = substr($str, $start, $length);
$temp = explode($sub, $str);
$result = count($temp) - 1;
return $result;
}
echo my_substr_count('sabhfdsapohdaadbabaabkdksaabb','ab');//4
echo my_substr_count('sabhfdsapohdaadbabaabkdksaabb','ab',3);//3
echo my_substr_count('sabhfdsapohdaadbabaabkdksaabb','ab',0,5);//1
{
if(!$length){
$length = strlen($str) - $start;
}
$str = substr($str, $start, $length);
$temp = explode($sub, $str);
$result = count($temp) - 1;
return $result;
}
echo my_substr_count('sabhfdsapohdaadbabaabkdksaabb','ab');//4
echo my_substr_count('sabhfdsapohdaadbabaabkdksaabb','ab',3);//3
echo my_substr_count('sabhfdsapohdaadbabaabkdksaabb','ab',0,5);//1
全部回答
- 1楼网友:人间朝暮
- 2021-02-28 03:38
为啥不可以?
function sb_co($c1,$c2,$c3,$c4){
return substr_count($c1,$c2,$c3,$c4);
}
- 2楼网友:往事隔山水
- 2021-02-28 03:00
function mySubstrCount($str, $search) {
$len1 = strlen($str);
$len2 = strlen($search);
$tmp = '';
$i = $count = 0;
while ($len1 > $i) {
$tmp1 = substr($str, $i, $len2);
if ($tmp1 == $search
)$count++;
$tmp.=$tmp1;
$i = $i + $len2;
}
return $count;
}
echo mySubstrCount('abcdrjfhgjyaatabcay', 'ab');
//更简单的办法
function mySubstrCount2($str, $search) {
$tmp=explode($search, $str);
$count=count($tmp)-1;
return $count;
}
echo mySubstrCount2('abcdrjfhgjyaatabcay', 'ab');
分拿来^_^
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯