Hello everyone,
I would like to count the number of words that do *not* use any of the words in array called $frags.
I had used the same code to count how many words that begin with each of those words.
Now, I would like to find out how many words in $mystring that do use any of the words in array called $frags.
Your assistance is greatly appreciated.
$mystring = "Some apple is eating my orange but not my watermelon.";
Code: Select all
<?php
$mystring = "Some apple is eating my orange but not my watermelon about.";
function notInList($string)
{
$frags = array('as', 'is', 'about', 'us', 'at');
$words = explode(' ', strtolower($string));
$count = 0;
$replace = array(',','.','\'','!'); //replace common punctuation so that it can still recognize words
foreach($words as $word)
{
if(in_array(str_replace($replace, '', $word), $frags))
{
echo str_replace($replace, '', $word) . '<br/>';
$count++;
}
}
return $count;
}
echo notInList($mystring) . " words.";
?> pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: