zintani Posted October 9, 2011 Share Posted October 9, 2011 Hi Guys, <?php $file = "I went back home to see my family as I was studying in China. By the time I arrived home I was so hungry and the weather was cloudy."; $words = array("see", "reader", "stud", "China", "cloudy", "hungry", "answer", "el", "prefer"); preg_match_all('/\b(' . implode("|", array_map("preg_quote", $words)) . ')/i', $file, $foundwords); foreach ($foundwords[0] as &$value) { echo "<br/>$value</br>"; } $sim = array_count_values($foundwords[0]); print_r ($sim); print_r(count ($sim)); $max = max ($sim); echo "<br/>".$max."<br/>"; foreach ($sim as $key=> $value) { $norm =($value/$max); echo $key. " = $norm </br>"; } ?> and the results of this is as follows see stud China hungry cloudy 5 Array ( [see] => 1 [stud] => 1 [China] => 1 [hungry] => 1 [cloudy] => 1 ) 5 1 see = 1 stud = 1 China = 1 hungry = 1 cloudy = 1 and my purpose is to show the whole $words = array("see", "reader", "stud", "China", "cloudy", "hungry", "answer", "el", "prefer"); and to write 0 if they don't appear and one if they appear. NOT just what PREG_MATCH_ALL brings. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 9, 2011 Share Posted October 9, 2011 I am not sure if this is what your trying to do... <?php $file = "I went back home to see my family as I was studying in China. By the time I arrived home I was so hungry and the weather was cloudy."; $words = array("see", "reader", "stud", "China", "cloudy", "hungry", "answer", "el", "prefer"); $foundWords = array(); foreach($words as $word){ $word = preg_quote($word); if(preg_match("/$word/i", $file)) $foundWords[] = $word; } print_r($foundWords); ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 10, 2011 Share Posted October 10, 2011 $file = "I went back home to see my family as I was studying in China. By the time I arrived home I was so hungry and the weather was cloudy."; $words = array("see", "reader", "stud", "China", "cloudy", "hungry", "answer", "el", "prefer"); $pattern = '/\b(' . implode("|", array_map("preg_quote", $words)) . ')\b/i'; preg_match_all($pattern, $file, $matches); //Remove duplicates and convert to lower case for matching $found_words = array_map('strtolower', array_unique($matches[0])); //Show all words, indicating matches foreach ($words as $word) { $found = (in_array(strtolower($word), $found_words)) ? 1 : 0; echo "$word : $found</br>"; } Quote Link to comment Share on other sites More sharing options...
zintani Posted October 12, 2011 Author Share Posted October 12, 2011 Thanks for both of you. The second answer was what I intended. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.