私はこれがあなたが望むものだと信じています。
PHP:
<?php
$words = array('to','be','not');
$str = "to be or not to be. what to be. everything else to be.";
$values = array_count_values(str_word_count($str, 1));
foreach($words as $word){
echo '"'.$word.'" appeared ';
if(isset($values[$word])){ echo $values[$word]; }else{ echo '0'; }
echo ' times';
}
?>
ハイライト付きPHP:
<?php
$words = array('to','be','not');
$str = "to be or not to be. what to be. everything else to be.";
$nStr = $str;
$values = array_count_values(str_word_count($str, 1));
foreach($words as $word){
$nStr = str_replace($word,"<span style='background-color:#FEEFB3;'>".$word."</span>",$nStr);
echo '"'.$word.'" appeared ';
if(isset($values[$word])){ echo $values[$word]; }else{ echo '0'; }
echo ' times ';
}
echo '<br/>'. $nStr;
?>