首页  >  文章  >  后端开发  >  在PHP中编写一个程序,用于查找一个字符在字符串中出现的次数

在PHP中编写一个程序,用于查找一个字符在字符串中出现的次数

WBOY
WBOY转载
2023-08-20 18:37:06787浏览

在PHP中编写一个程序,用于查找一个字符在字符串中出现的次数

示例

 演示

<?php
   $str = "welcome to tutorials point";
   $str = str_replace(" ","",$str);
   $arr = str_split($str);
   foreach ($arr as $key =>$val){
      if (!isset($output[$val])){
            $output[$val] = 1;
      }
      else{
         $output[$val] += 1;
      }
   }
   foreach($output as $char =>$number){
      echo $char." ".&#39;appears &#39;. $number." ".&#39;times&#39;."</p><p>";
   }
?>

Output

w appears 1 times
e appears 2 times
l appears 2 times
c appears 1 times
o appears 4 times
m appears 1 times
t appears 4 times
u appears 1 times
r appears 1 times
i appears 2 times
a appears 1 times
s appears 1 times
p appears 1 times
n appears 1 times

以上是在PHP中编写一个程序,用于查找一个字符在字符串中出现的次数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除