14

How can we count space between text in PHP?

example: hi how are you?

spaces: 3

Is there a way to count spaces?

Language: Only PHP

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

4 Answers4

49

Use this:

substr_count($text, ' ');
gahooa
  • 131,293
  • 12
  • 98
  • 101
2

You're looking for preg_match_all.

$numSpaces = preg_match_all('/[ ]/', $testStr, $matches);
nikc.org
  • 16,462
  • 6
  • 50
  • 83
1
$arr = count_chars($str,1);
echo $arr[32];
jimyi
  • 30,733
  • 3
  • 38
  • 34
0
$text   = "Biography of the author of Riyād al-Sālihīn";

$spaces = substr_count($text,' ') ?: 0; // if override spaces from sentences

$data   = substr($text,0,$length + $spaces); //`enter code here