Write a program in PHP to find the number of times a character appears in a string

WBOY
Release: 2023-08-20 18:38:01
forward
773 people have browsed it

Write a program in PHP to find the number of times a character appears in a string

Example

Demonstration

<?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>";
   }
?>
Copy after login

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
Copy after login

The above is the detailed content of Write a program in PHP to find the number of times a character appears in a string. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!