1

I have tried to use preg_match to match the spaces but it returns one always. How can I get the number of spaces in this string? In this case it should return 8. Any use PHP function is ok.

$string = "The quick brown fox jumps over the lazy dog";
John Robertson
  • 1,486
  • 13
  • 16

2 Answers2

5

Actually you could also use substr_count for this:

$string = "The quick brown fox jumps over the lazy dog";
echo substr_count($string, ' '); // 8
Kevin
  • 41,694
  • 12
  • 53
  • 70
1

use this:

substr_count($text, ' ');

substr_count — Count the number of substring occurrences

munsifali
  • 1,732
  • 2
  • 24
  • 43