Programming Tutorials

(追記) (追記ここまで)

Count occurrences of a character in a String in PHP

By: Kani in PHP Tutorials on 2012年04月05日 [フレーム]

Here is another simple example for - for loops. Thsi PHP code shows how to search for a particular character in a String and count the number of occurrences of that character in that string.

<?php
$text="Welcome to PHP";
$searchchar="e";
$count="0"; //zero
for($i="0"; $i<strlen($text); $i=$i+1){

if(substr($text,$i,1)==$searchchar){
$count=$count+1;
}
}
echo $count
?>

this will count how many times the character "e" occurs in that text (Welcome to PHP).




(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /