So I'm new to regex and I've been trying to experiment with a word counter pattern in PHP. I use preg_match_all to get the count of spaces with the following pattern: "/[^(\s\s|\s\s\s)][(\B\s\B)]/"
This seems like a backwards approach, and it doesn't always accurately match the number of words.
I want the following to be true:
"This is a statement" = 4 words "This is a statement " = 4 words " This is a statement" = 4 words " This is a statement " = 4 words ... etc...
I feel like there's gotta be a much more logical solution than my approach, and I'm just not seeing.
I don't think that is as precise. The regex method will count the number words accurately regardless of extra spaces. Exploding by spacing could lead to an inaccurate count unless you code for empty subscripts. Ex:
$sText = " This is a simple string with extra spaces ";