从零开始学php 第六天

昨晚,Lee给俺布置了个简单的作业,是查询一个字符串在另一个字符串中出现的次数,提示用函数是str_replacestrlen.
昨晚睡着睡着突然想起了一个方法.

$target = "wc";//要数的字符串
$source = "kjhwclkjlkjwckjlkjlkjugwckjlijwcikjoijwlkjc";//目标源字符串
$str = str_replace($target, "X", $source);//用少一个字母的字符串来替代,如果是wcx的话就用两个字母来替代,比如Xy,但是应该不能用中文,strlen函数对中文长度截取好像一个中文字计数为2
$n = strlen($source)-strlen($str);
echo $source." 重复出现了 ".$target." “.$n.” 次”;
?>

100%纯原创,高兴

3 Responses to 从零开始学php 第六天

  1. 阿强 says:

    :em16: ,一起学习!

  2. 赵明亮 says:

    嗯,理论应该是.我的意思是自己先用人工偷懒了一把

  3. dancebear says:

    $n = strlen($source)-strlen($str);
    $n=$n/strlen($str);
    这样才对吧!否则str就只能为长度为1的字符了

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>