Tuesday, September 23, 2008

The substr_count() function

This function could be used to count the number of times a sub-string occurs within a given string.

Syntax: substr_count(string,sub-string[,start,length]);

sub-string- the string to be searched for
start – that position in the larger string from where the search should begin
length – length of the search from the starting position

If start and length parameters are not specified, substr_count() carries out search in the entire string.
Example input:
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr_count($str1,"PHP",3,20);
echo "
";
echo substr_count($str1,"PHP",3,10);
echo "
";
echo substr_count($str1,"i");
Example output:
This is a simple PHP program 1 0 3

No comments: