1

How can I check if a string is a multi word sentence or just a single word?

I tried by splitting by a space so if it is one word, there will be only one word in the array and if there is more, then they will all be in the array.

But, when I try to split by a space and run through that array, I am getting no output.

Here is the code:

$input = "the quick brown fox jumped over the lazy dog";

$sentence = explode(" ", $input);

foreach($sentence as $item){
    echo $item;
}

The above is giving me no output.

So, I have 2 questions:

  1. How can I detect if a string is composed of multiple words in an if statement?
  2. Why isn't my above code splitting the sentence into an array with the words?
6
  • There is no error in your code, so I don't know... Commented Jun 5, 2013 at 23:34
  • Code, as is, works fine. You must have lost something in translation from your implementation to your question. Commented Jun 5, 2013 at 23:35
  • 3
    It would be more efficient to just check for the presence of a " ".
    – alex
    Commented Jun 5, 2013 at 23:37
  • @BjørnKjos-Hanssen That is why I don't know. Commented Jun 5, 2013 at 23:55
  • @MikePurcell Must be. Commented Jun 5, 2013 at 23:56

6 Answers 6

5

Haven't run the metrics between this or hek2mgl's solution, but this should be faster:

if (stripos($input, ' ') !== false) { echo 'ZOMG I HAS WORDS'; }

Also, as mentioned in the comments, the code you posted works as expected.

2
  • 1
    The more important part is that it's easier to comprehend the code's intention.
    – alex
    Commented Jun 6, 2013 at 0:05
  • @alex: Agreed. That's why I always curl up in the fetal position when I see one liners like: $unicorns = array_flip(array_keys(array_intersect_keys(array_flip($arr1), $arr2)); Commented Jun 6, 2013 at 0:07
2

For reference there is also str_word_count($string) which will also provide the number of words within a string.

1

How can I detect if a string is composed of multiple words in an if statement?

if(count(explode(' ', $str)) > 0) { echo 'sentence'; }

Why isn't my above code splitting the sentence into an array with the words?

The code should work. I got (after adding a newline to the echo):

the
quick
brown
fox
jumped
over
the
lazy
dog
0

Your code should be working fine in order to check if there is more than one word you need to check like below

$input = "the quick brown fox jumped over the lazy dog";

$sentence = explode(" ", $input);

echo "<pre>";
var_dump($sentence);
echo "</pre>";


if(count($sentence)){
echo "we have more than one word!";
}
-1

To check if there are spaces you can use a regex like:

$spaces = preg_match('/ /',$input);

This should return a 1 or 0 depending on if there is a space.

if( $spaces == 1 )
  // there is more than one word
else
  // there is only one word with no spaces

Your code looks error free other than that.

3
  • 1
    There is no need for a regular expression to search for a single character.
    – Paul
    Commented Jun 5, 2013 at 23:40
  • They asked for a way to check if there were multiple words in the input. This is a working method to do so.
    – dremme
    Commented Jun 5, 2013 at 23:44
  • True, hence why I didn't downvote. But $spaces = preg_match('/ /',$input); is really just a slow version of $spaces = strpos($input, ' ') !== false which I would upvote.
    – Paul
    Commented Jun 5, 2013 at 23:49
-1

Heres some pseudocode for it:

String input = "The quick brown fox jumped over the lazy dog"

Array[] words = String.split(input,' ');
if(words.length > 1)

print " more than 1 word"

else

print "1 word"

Not the answer you're looking for? Browse other questions tagged or ask your own question.