在PHP搜索脚本中突出显示关键字

时间:2011-07-04 20:17:38

标签: php mysql html sql

我有一个PHP搜索脚本,用于查询MySQL数据库,然后通过HTML解析结果以允许CSS样式。我希望脚本突出显示用户搜索的结果中的所有关键字。我怎么能用PHP做到这一点?

我的PHP脚本是:

<?php

mysql_connect("localhost","username","password");
mysql_select_db("database");

if(!empty($_GET['q'])){
$query=mysql_real_escape_string(trim($_GET['q']));
$searchSQL="SELECT * FROM links WHERE `title` LIKE '%{$query}%'  LIMIT 8";
$searchResult=mysql_query($searchSQL);

while ($row=mysql_fetch_assoc($searchResult)){
    $results[]="<a href='{$row['url']}' class='webresult'><div class='title'>{$row['title']}</div><div class='desc'>{$row['description']}</div><div class='url'>{$row['url']}</div></a>";
}

if(empty($results)){
echo 'No results were found';
} else {
echo implode($results);
}
}

?>

4 个答案:

答案 0 :(得分:0)

您可以使用str_replace。对于用户使用的每个关键字,将其放入$search数组中,并将其放入$replace数组中,但在后者中包含带有span标记的$search = array('apple', 'orange'); $replace = array(); foreach ($search as $word) { $replace[] = "<span class='highlight'>$word</span>"; } $string = str_replace($search, $replace, $string); 标记,您可以使用以后使用CSS的样式。例如:

$query

编辑:假设$search = explode(' ', $query); 只包含由单个空格分隔的关键字,您可以通过这种方式获取搜索数组(使用explode),

$query

或者,如果您想添加更复杂的逻辑来处理+变量之外的关键字(就像使用$queryTerms = explode(' ', $query); $search = array(); foreach ($queryTerms as $term) { // do some processing of the $term (like delete "+"?) // ... $search[] = $processedTerm; } 这样的查询运算符),您可以使用for循环:

{{1}}

答案 1 :(得分:0)

简单地说,你可以在这里调整循环:

$searchvar = trim($_GET['q']);
while ($row=mysql_fetch_assoc($searchResult)){
    $description = str_replace($searchvar, '<span class="highlight">'.$searchvar."</span>", $row['description']);
    $results .="<a href='{$row['url']}' class='webresult'>
    <div class='title'>{$row['title']}</div>
    <div class='desc'>{$description}</div>
    <div class='url'>{$row['url']}</div></a>";
}

为了让它好一点:

$searchvar = explode(" ", trim($_GET['q'])); //puts each space separated word into the array.      
while ($row=mysql_fetch_assoc($searchResult)){
    $description = $row['description'];
    foreach($searchvar as $var) $description = str_replace($var, '<span class="highlight">'.$var."</span>", $description);
    $description = str_replace($searchvar, '<span class="highlight">'.$searchvar."</span>", $row['description']);
    $results .="<a href='{$row['url']}' class='webresult'>
    <div class='title'>{$row['title']}</div>
    <div class='desc'>{$description}</div>
    <div class='url'>{$row['url']}</div></a>";
}

第二个的好处是,如果用户键入“ipod toudch yellow”,您将搜索“ipod”,“toudch”和“yellow”,这将否定该类型并使结果更加通用。

你需要换单:

like '%query%'

foreach(explode(" ", trim($_GET['q']) as $searchvar) $where[] = "like '%$searchvar%'";
$wheresql = implode(" OR ", $where);

获取要在sql中查找的每个搜索“单词”,否则您将使用无关的突出显示进行有限的搜索。

答案 2 :(得分:0)

我还发现这个解决方案突出显示结果的每个字:

$text = $searchresults;
class highlight
{
  public $output_text;

  function __construct($text, $words)
  {
    $split_words = explode( " " , $words );

    foreach ($split_words as $word)
    {
      $text = preg_replace("|($word)|Ui" , "<b>$1</b>" , $text );
    }
  $this->output_text = $text;
  }
}
$highlight = new highlight($searchresults, $keywords);
echo $highlight;

如果可以提供帮助,

此致

最高

答案 3 :(得分:0)

您可以使用正则表达式或简单地使用str替换来查找特定字符串并在其周围添加范围:

while ($row=mysql_fetch_assoc($searchResult)){
    $str ="<a href='".$row['url']."' class='webresult'>";
    $str .="<div class='title'>".$row['title']."</div>";
    $str .="<div class='desc'>";
    $str .= str_replace($query,"<span class='hightlighted'>".$query."</span>",$row['description']);
    $str .="</div><div class='url'>".$row['url']."</div></a>";
    $result[] = $str;
}

现在是css:

span.highlighted {
   background-color: yellow;
}