Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/248751-show-the-whole-array/
Share on other sites

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

$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>";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.