How to echo error from foreach loop?

Error on my PHP code?

  • this is a sample program which classifies a mail based on spam content. <?php function is_spam($text, $file, $split = ':', $regex = false){ $handle = fopen($file, 'rb'); $contents = fread($handle, filesize($file)); fclose($handle); $lines = explode("\n", $contents); $arr = array(); foreach($lines as $line){ list($word, $count) = explode($split, $line); if($regex) $arr[$word] = $count; else $arr[preg_quote($word)] = $count; } preg_match_all("~".implode('|', array_keys($arr))."~", $text, $matches); $temp = array(); foreach($matches[0] as $match){ if(!in_array($match, $temp)){ $temp[$match] = $temp[$match] + 1; if($temp[$match] >= $arr[$word]) return true; } } return false; } $file = 'spam.txt'; $str = 'This string has the word viagra and sex get viagra see viagra'; if(is_spam($str, $file)) echo 'this is spam'; else echo 'this is not spam'; ?> SPAM.TXT viagra:3 sex:3 porn:2 boobs:2 law firm:4 Error: Notice: Undefined index: viagra in C:\Documents and Settings\paramesh\Desktop\xampp\htdocs\n… on line 19 Notice: Undefined index: sex in C:\Documents and Settings\paramesh\Desktop\xampp\htdocs\n… on line 19 this is not spam

  • Answer:

    seems you are confused between match, matches, matches[0] Check

Paramesh C at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.