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

Member Avatar for diafol

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.

wow what a difficult problem, worth asking

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 ?

Member Avatar for iamthwee

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

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

the method seems to be working ok thanks anyway

Member Avatar for iamthwee

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

Member Avatar for diafol

Are we solved?

Member Avatar for iamthwee

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

yes it is solved thanks

Member Avatar for diafol

Ok, mark it solved (link below)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.