0

i have 2 methods to check if words appear within a piece of text, the method only checks for a word once but i want it to check more than once

while ($pos_words = mysql_fetch_assoc($pos)) {
    if (strpos($review_text, $pos_words['word']) !== FALSE) {
        $good++;
    }
}


//Get negative words and check it against the review, minus 1 if word found 
while ($neg_words = mysql_fetch_assoc($neg)) {
    if (strpos($review_text, $neg_words['word']) !== FALSE) {
        $bad++;
    }
}

anyone know how this can be done ? anyhelp would be appreciated

5
Contributors
13
Replies
14
Views
5 Years
Discussion Span
Last Post by diafol
Featured Replies
0

There are a million ways in which to do this.

$cnt_r = array_count_values(array_map('mb_strtolower',str_word_count( $review_text, 1))) ;

$good=0;$bad=0;
while($check = mysql_fetch_assoc($pos)){
  $lower = mb_strtolower($check['word']);
 if(isset($cnt_r[$lower])){
	$good+= $cnt_r[$lower]; 
 }else{
	$bad++; 
 }
}
echo "Good words: " . $good . " and Bad words: " . $bad;

It may look complicated, but the top bit:
1. gets all words in the text to an array
2. converts all words to lowercase
3. counts all individual words

I also used mb_strtolower to that it *should* support international characters

The only issue I can see is if you have duplicate values in the DB field, 'word' - but you can deal with this with a SELECT DISTINCT maybe.

Edited by diafol: n/a

1

who exactly told you to rattle your cage

Votes + Comments
I am Iamthwee and I approve of this post
0

thanks i will give that a try but isnt there a way of using a for loop for it ?

0

Let's make it the easy way.. Non case-sensitive

$text = "This is a kickass day.Damn...What a Day !";
$needle = "Day";
strtolower($text);
strtolower($needle);
$tokens = explode($needle, $text);
$found = count($tokens);

So $found is what you want to know, if i understood your problem correctly.
Laters :)

Votes + Comments
Very simple, and works!
no
0

@Robert

I like the simplicity of that but it is flawed... Do you know why and where?

...Additionally, you have to know what the repeated word is first, which is impractical under real circumstances...

-But if the OP knows what the repeated word is then you don't need to design a more robust function/method.

Edited by iamthwee: n/a

0

>Are we solved?

Depends if the OP goes for Robert's solution. If he does he'll come undone because the logic is flawed.

And just throwing my solution, if the OP knows what the word is he wants to be counted he could use...

<?php

function strpos_recursive($haystack, $needle, $offset = 0, &$results = array()) 
{               
    $offset = strpos($haystack, $needle, $offset);
    if($offset === false) 
    {
        return $results;           
    } else 
    {
        $results[] = $offset;
        return strpos_recursive($haystack, $needle, ($offset + 1), $results);
    }
}


$string = 'This is a kickass ..DAy.Damn...    		What a DaY';
$search = 'day';
$found = strpos_recursive(strtolower($string), strtolower($search));

if($found) 
{
  $count = 0;  
  foreach($found as $pos) 
  {
       $count++;     
  }   
} else {
    echo '"'.$search.'" not found in "'.$string.'"';
}
 echo "Found $count";
?>

Which avoids complications with words separated by more than one space, tabs or preceeded by fullstops such as .....Day.

Edited by iamthwee: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.