首頁 >後端開發 >php教程 >在PHP中編寫一個程序,用於尋找一個字元在字串中出現的次數

在PHP中編寫一個程序,用於尋找一個字元在字串中出現的次數

WBOY
WBOY轉載
2023-08-20 18:37:06802瀏覽

在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刪除