views:

507

answers:

3

how can we count space between text in php ?

example: hi how are you?

spaces: 3

is there any way to count spaces ?

Language : Only PHP

A: 
$arr = count_chars($str,1);
echo $arr[32];
jimyi
This way still works, but it's really a round-about way, since the substr_count() function exists.
James Skidmore
+9  A: 

Use this:

substr_count($text, ' ');
gahooa
A: 

You're looking for preg_match_all.

$numSpaces = preg_match_all('/[ ]/', $testStr, $matches);
nikc