最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to bold keyword in php mysql search? - Stack Overflow

programmeradmin5浏览0评论

I want want my output like this when I search a keyword like

"programming"

php programming language

How to do this in php mysql?

Any idea?

I want want my output like this when I search a keyword like

"programming"

php programming language

How to do this in php mysql?

Any idea?

Share Improve this question asked Oct 12, 2010 at 3:17 devzonedevzone 3051 gold badge12 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Just perform a str_replace on the returned text.

$search = 'programming';
// $dbContent = the response from the database

$dbContent = str_replace( $search , '<b>'.$search.'</b>' , $dbContent );

echo $dbContent;

Any instance of "programming", even if as part of a larger word, will be wrapped in <b> tags.

For instances where more than one word are used

$search = 'programming something another';
// $dbContent = the response from the database

$search = explode( ' ' , $search );
function wrapTag($inVal){
  return '<b>'.$inVal.'</b>';
}
$replace = array_map( 'wrapTag' , $search );

$dbContent = str_replace( $search , $replace , $dbContent );

echo $dbContent;

This will split the $search into an array at the spaces, and then wrap each match in the <b> tags.

You could use <b> or <strong> tags (See What's the difference between <b> and <strong>, <i> and <em>? for a dicussion about them).

$search = @$_GET['q'];
$trimmed = trim($search);

function highlight($req_field, $trimmed)  //$req_field is the field of your table
{
        preg_match_all('~\w+~', $trimmed, $m);
        if(!$m)
            return $req_field;
        $re = '~\\b(' . implode('|', $m[0]) . ')\\b~';
        return preg_replace($re, '<b>$0</b>', $req_field);
}

  print highlight($req_field, $trimmed);

In this way, you can bolden the searched keywords. Its quite easy and works well.

The response is actually a bit more plicated than that. In the mon search results use case, there are other factors to consider:

  • you should take into account uppercase and lowercase (Programming, PROGRAMMING, programming etc);
  • if your content string is very long, you wouldn't want to return the whole text, but just the searched query and a few words before and after it, for context;

This guy figured it out:

//$h = text
//$n = keywords to find separated by space
//$w = words near keywords to keep

function truncatePreserveWords ($h,$n,$w=5,$tag='b') {
    $n = explode(" ",trim(strip_tags($n))); //needles words
    $b = explode(" ",trim(strip_tags($h))); //haystack words
    $c = array();                       //array of words to keep/remove
    for ($j=0;$j<count($b);$j++) $c[$j]=false;
    for ($i=0;$i<count($b);$i++) 
        for ($k=0;$k<count($n);$k++) 
            if (stristr($b[$i],$n[$k])) {
                $b[$i]=preg_replace("/".$n[$k]."/i","<$tag>\\0</$tag>",$b[$i]);
                for ( $j= max( $i-$w , 0 ) ;$j<min( $i+$w, count($b)); $j++) $c[$j]=true; 
            }   
    $o = "";    // reassembly words to keep
    for ($j=0;$j<count($b);$j++) if ($c[$j]) $o.=" ".$b[$j]; else $o.=".";
    return preg_replace("/\.{3,}/i","...",$o);
}

Works like a charm!

发布评论

评论列表(0)

  1. 暂无评论