Jump to content

words not in list


sonnieboy

Recommended Posts

Hello everyone,

 

I would like to count how words that  do *not* have any of the words in array $frags in them.

 

Can you assist, please?<?php

$mystring = "Some apple is eating my orange but not my watermelon.";

 

function notInList($string) {

 

  $frags = array('as', 'is', 'about', 'us', 'at');

  $parts = explode(' ', strtolower($string));

  $count = 0;

 

  foreach ($parts as $part) {

    if (in_array($part[0], $frags)) {

        echo "$part<br />";

        $count++;

    }

  }

 

  return $count;

}

 

echo notInList($mystring)." words";

?>

 

I am pretty much a newbie to php; sorry.

Thanking you in advance.

 

 

 

Link to comment
Share on other sites

<?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.";
?>

 

I commented where you had made your mistake. :)

 

EDIT: I also made it to remove punctuation so that it can still recognize the word even if it has an ',!. at the end of it. :)

Link to comment
Share on other sites

Thanks alot xnowandtheworldx for the prompt response.

 

I wanted to see a count of words that don't have any of these words:

 

$frags = array('as', 'is', 'about', 'us', 'at');

 

Thanks,

 

Ah, okay, sorry about that, well, if you can't figure out how to change it post here, and I will post the updated code and let you know how I set it up. :)

Link to comment
Share on other sites

can anyone, please help, please?

 

<?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));
    $replace = array(',','.','\'','!'); //replace common punctuation so that it can still recognize words
    $count = 0;
    foreach($words as $word) {
        if(!in_array(str_replace($replace, '', $word), $frags)) { //All you had to do was add an ! in front of the in_array, so essentially, it is now trying make sure that the words in $frags are NOT in the string...if that makes sense?
            echo str_replace($replace, '', $word) . '<br/>';
            $count++;
        }
    }
    return $count;
}

echo notInList($mystring) . " words.";
?>

 

I believe this is what you wanted, if it's not just let me know, and I'll see what I can do. :)

Link to comment
Share on other sites

xnowandtheworldx,

 

Thank you very, very much.

 

We are almost there.

 

When I added the actual text that we use here at work, then some works that contain some of those are still showing.

 

Take as an example the letter 'a'.

 

If you add the letter a into the array $frags, then that means that the word apple should not appear on the list because it contains the word 'a' but it still appears on that list.

 

I really appreciate your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.