-6

How would You check if string contains exactly 2 non-adjacent spaces in PHP ?

f.e. 'John Wayne Washington' -> TRUE (2 spaces in string); 'John Wayne' -> FALSE (1 space)

Any regex pattern with explanation ?

Lars Fischer
  • 9,135
  • 3
  • 26
  • 35

2 Answers2

2
$string = "Bush      sucks       big    time";
$counter = count_chars($string,0);
echo $counter[32] . " spaces in the phrase.";
suman das
  • 357
  • 1
  • 12
  • 1
    Please provide an explanation alongside a code chunk so that others with a similar question can easily understand what is happening. As it stands now, this question is in a low quality posts review queue. – coatless Jul 17 '16 at 22:44
0

Use this pattern:

^[^ ]* [^ ]+ [^ ]*$

It matches a string with exactly 2 non-adjacent spaces.

logi-kal
  • 7,107
  • 6
  • 31
  • 43