Count the number of times "world" occurs in the string in PHP
Description
The following code shows how to count the number of times "world" occurs in the string.
Example
/*from www .jav a 2 s. c o m*/
<?php
echo substr_count("Hello world. The world is nice","world");
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is PHP"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is PHP"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
//Overlapped substrings:
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
//If the start and length parameters exceeds the string length, this function will output a warning
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
The code above generates the following result.