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

6 Years
Discussion Span
Last Post by diafol
Featured Replies

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.

Edited by diafol: n/a


who exactly told you to rattle your cage

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

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


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

Votes + Comments
Very simple, and works!


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


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

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.