简体   繁体   English

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

[英]Highlighting keywords in PHP search script

I have a PHP search script that queries a MySQL database and then parses the results through HTML to allow CSS styling.我有一个 PHP 搜索脚本,它查询 MySQL 数据库,然后通过 HTML 解析结果以允许 Z2C56C360580421DFBED23 造型。 I want the script to highlight all of the keywords in the results that the user has search for.我希望脚本突出显示用户搜索的结果中的所有关键字。 How can I do this with PHP?如何使用 PHP 做到这一点?

My PHP script is:我的 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);
}
}

?>

You can use str_replace .您可以使用str_replace For each keyword the user uses, put it into the $search array, and also put it into a $replace array, but surround with with a classed span tag in the latter, which you can style with CSS later.对于用户使用的每个关键字,将其放入$search数组中,也将其放入$replace数组中,但在后者中用一个分类的span标签包围,稍后您可以使用 CSS 对其进行样式设置。 For example:例如:

$search = array('apple', 'orange');
$replace = array();
foreach ($search as $word)
{
    $replace[] = "<span class='highlight'>$word</span>";
}

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

EDIT: assuming that $query just contains keywords delimited by a single whitespace, you could get the search array this way (with explode ),编辑:假设$query只包含由单个空格分隔的关键字,您可以通过这种方式获取搜索数组(使用explode ),

$search = explode(' ', $query);

Or, if you want to add more complex logic for processing the keywords out of the $query variable (like if you use query operators like + ), you could use a for loop:或者,如果您想添加更复杂的逻辑来处理$query变量中的关键字(例如,如果您使用像+这样的查询运算符),您可以使用 for 循环:

$queryTerms = explode(' ', $query);
$search = array();
foreach ($queryTerms as $term)
{
    // do some processing of the $term (like delete "+"?)
    // ...

    $search[] = $processedTerm;
}

Simplistically you could adapt the loop here:简单地说,您可以在此处调整循环:

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

To make it a little better:为了让它更好一点:

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

The benefit of the second one there is that if a user types in "ipod toudch yellow" you will be searching for "ipod", "toudch" and "yellow" which would negate the type and make the results more general.第二个的好处是,如果用户输入“ipod touch yellow”,您将搜索“ipod”、“toudch”和“yellow”,这将否定类型并使结果更普遍。

You would need to exchange the single:您需要交换单曲:

like '%query%'

with

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

to get each search "word" to be looked for in the sql or you will have a limited search with a unrelated highlight.要在 sql 中查找每个搜索“单词”,否则您将进行有限搜索,并带有不相关的突出显示。

i also found this solution to highlight each word of the results:我还找到了这个解决方案来突出结果的每个单词:

$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;

In case that could help,如果有帮助,

Regards,问候,

Max最大限度

you can use regex or simply str replace to look for a particular string and add a span around it:您可以使用正则表达式或简单地使用 str replace 来查找特定字符串并在其周围添加一个跨度:

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

now the css:现在是 css:

span.highlighted {
   background-color: yellow;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM