首先显示最佳匹配

时间:2016-01-07 11:39:23

标签: php preg-match

<?php
if(!empty($match)) {
foreach ($match as $r) {
    echo "<li>$r</li>\n";
	
}
}
?>

如果我有多个preg_match且一个有更多与输入匹配的单词,我该如何制作它以便显示最匹配的单词。现在,它只是按顺序显示它。这是它输出的方式

 function changeIframeSrc(index){

   var iframeid = document.getElementById('changingIframe');
   iframeid.setAttribute('src', sourceList[index]);

 }

2 个答案:

答案 0 :(得分:1)

试试这个:

$input = "work is hard";            
$reg = array( "First match"=>"/(work|shift|useful)/i","second match"=>"/(how|work|can|be|really|really|hard)/i");
foreach($reg as $index=>$val){
    $count[] = preg_match_all($val,$input);
}
foreach($count as $i){
    $max = max($count);
    $key = array_search($max,$count); 
    $allKeys = array_keys($reg);
    echo "The Best Match is:  ".$allKeys[$key]."<br>"; 
    $del = array_search($max,$count);
    unset($count[$del]);
}

答案 1 :(得分:0)

将输入字符串拆分为单词数组,并将array_intersect与您感兴趣的单词组一起使用,并比较结果数组的长度以确定最佳匹配。