Page 1 of 1
Can you help with words count, please?
Posted: Tue Mar 09, 2010 9:02 am
by simflex
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:
Posting Code in the Forums to learn how to do it too.
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:
Posting Code in the Forums to learn how to do it too.
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 9:45 am
by pickle
Use
str_replace() to replace any of the words found in your array with an empty string. Then use
str_word_count() to get the word count.
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 10:12 am
by simflex
First, thank you very much Pickle for that correction.
This is my first post here and I must admit that I looked at posting Code rules but didn't read long enough to pick up those important hints.
Also, can you please get me started?
I am pretty much a newbie to this.
Been a classic ASP programmer all my life.
I am just now transitioning to PHP and .net.
Again, many thanks.
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 10:47 am
by pickle
str_replace() can accept as an array, the list of substrings to replace. Pass your array of words as that array, and an empty string as the second argument, and it will essentially remove all occurrences of those words. Run the resulting string through str_word_count() & you'll get your word count.
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 11:05 am
by AbraCadaver
pickle wrote:Use
str_replace() to replace any of the words found in your array with an empty string. Then use
str_word_count() to get the word count.
Unfortunately this will replace the 'at' in w'at'ermelon as well.
Here is a quick one that assumes that you want numbers - _ as well. Take note that this splits on one or more spaces, so if there are multiple spaces in a row you won't have problems like explode().
Code: Select all
$count = count(array_diff(preg_split('/[ ]+/', preg_replace('/[^\d\w\s]/', '', $mystring), null, PREG_SPLIT_NO_EMPTY), $frags));
I was bored...
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 1:04 pm
by simflex
Hi,
Thanks much for all the assistance,
Where should I put your code?
Besides, i am running into a ton of errors.
Please take a look. Again, thanks a lot.
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, '', $words), $frags))
{
echo str_replace($replace, '', $word) . '<br/>';
$count++;
}
}
return $count;
}
$count = count(array_diff(preg_split('/[ ]+/', preg_replace('/[^\d\w\s]/', '', $mystring), null, PREG_SPLIT_NO_EMPTY), $frags));
echo notInList($mystring) . " words.";
?>
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 1:21 pm
by AbraCadaver
It replaces all of your code:
Code: Select all
function notInList($string) {
$frags = array('as', 'is', 'about', 'us', 'at');
$count = count(array_diff(preg_split('/[ ]+/', preg_replace('/[^\d\w\s]/', '', $string), null, PREG_SPLIT_NO_EMPTY), $frags));
return $count;
}
$mystring = "Some apple is eating my orange but not my watermelon about.";
echo notInList($mystring) . " words.";
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 2:11 pm
by pickle
AbraCadaver wrote:Unfortunately this will replace the 'at' in w'at'ermelon as well.
So?
str_word_count() will still count 'wtermelon' as a word.
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 2:17 pm
by simflex
thank you very much. You are great.
Can you please tell me what function or syntax lists those words that don't contain those in array $frags?
I would like to list them, followed by the total number.
Again, thank you very much.
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 2:35 pm
by AbraCadaver
pickle wrote:AbraCadaver wrote:Unfortunately this will replace the 'at' in w'at'ermelon as well.
So?
str_word_count() will still count 'wtermelon' as a word.
I was going to concede and say, "Doh!", but now he wants the words
Code: Select all
function notInList($string) {
$frags = array('as', 'is', 'about', 'us', 'at');
$words = implode(' ', array_diff(preg_split('/[ ]+/', preg_replace('/[^\d\w\s]/', '', $string), null, PREG_SPLIT_NO_EMPTY), $frags));
return $words;
}
$mystring = "Some apple is eating my orange but not my watermelon about.";
$mywords = notInList($mystring);
echo str_word_count($mywords) . ' words. The words are: ' . $mywords;
Depends on how you want them. The above function will return them in a string like they were before. If you want them returned in array to work with as you want to, then:
Code: Select all
function notInList($string) {
$frags = array('as', 'is', 'about', 'us', 'at');
$words = array_diff(preg_split('/[ ]+/', preg_replace('/[^\d\w\s]/', '', $string), null, PREG_SPLIT_NO_EMPTY), $frags);
return $words;
}
$mystring = "Some apple is eating my orange but not my watermelon about.";
$mywords = notInList($mystring);
echo count($mywords) . ' words. The words are: ' . implode(' ', $mywords);
Re: Can you help with words count, please?
Posted: Tue Mar 09, 2010 8:58 pm
by simflex
Thank you very, very much AbraCadaver.
I really appreciate your help.