Jump to content

show the whole array


zintani

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

Archived

This topic is now archived and is closed to further replies.

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