Can you help with words count, please?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simflex
Forum Newbie
Posts: 14
Joined: Thu Nov 06, 2003 9:18 pm

Can you help with words count, please?

Post 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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can you help with words count, please?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simflex
Forum Newbie
Posts: 14
Joined: Thu Nov 06, 2003 9:18 pm

Re: Can you help with words count, please?

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can you help with words count, please?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Can you help with words count, please?

Post 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...
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simflex
Forum Newbie
Posts: 14
Joined: Thu Nov 06, 2003 9:18 pm

Re: Can you help with words count, please?

Post 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.";
?>
 
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Can you help with words count, please?

Post 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.";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can you help with words count, please?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simflex
Forum Newbie
Posts: 14
Joined: Thu Nov 06, 2003 9:18 pm

Re: Can you help with words count, please?

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Can you help with words count, please?

Post 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 8)

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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simflex
Forum Newbie
Posts: 14
Joined: Thu Nov 06, 2003 9:18 pm

Re: Can you help with words count, please?

Post by simflex »

Thank you very, very much AbraCadaver.

I really appreciate your help.
Post Reply