I want to check how many instances of the letter "a" appears in a certain string. What is the function for this? I need it to return an integer value.
+3
A:
You can use the function : substr_count
Example:
$str = "I love stackoverflow";
echo substr_count($str,'o'); // prints 3
codaddict
2010-03-23 06:28:43
+2
A:
I suppose substr_count could do the trick ;-)
For example, this portion of code :
$str = 'abcdazerty';
echo substr_count($str, 'a');
would get you the following output :
2
And, quoting :
int substr_count ( string $haystack ,
string $needle [, int $offset = 0
[, int $length ]] )
substr_count()returns the number of times theneedlesubstring occurs in thehaystackstring. Please note that needle is case sensitive.
Pascal MARTIN
2010-03-23 06:29:47
+1
A:
you can refer to PHP string functions manual to find any function you need, http://php.net/strings
Col. Shrapnel
2010-03-23 06:33:16
+1
A:
substr_count is probably the most straightforward. you can also do this
$str = "I love stackovaerflowa";
print count(explode("a",$str))-1;
ghostdog74
2010-03-23 06:42:46