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) {

//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) {

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

Member Avatar

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))) ;

while($check = mysql_fetch_assoc($pos)){
  $lower = mb_strtolower($check['word']);
	$good+= $cnt_r[$lower]; 
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.

who exactly told you to rattle your cage

commented: I am Iamthwee and I approve of this post +15

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

For loop, while loop... Does it matter?

i dont know, im not good at php otherwise i wouldn't be asking

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

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

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

commented: Very simple, and works! +3
commented: no -3

the method seems to be working ok thanks anyway


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.

Member Avatar

Are we solved?

>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...


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));

  $count = 0;  
  foreach($found as $pos) 
} 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.

yes it is solved thanks

Member Avatar

Ok, mark it solved (link below)