【问题标题】:Highlight search text in mysql php search在mysql php搜索中突出显示搜索文本
【发布时间】:2012-06-25 21:49:58
【问题描述】:

嗨,这是我的脚本,它可以很好地搜索我的数据库。 我有一个 form.html,用户在搜索框中输入关键字。

<form method="get" action="form.php">
<input type="text" name="searchterm" title="Enter your search term here" value="Enter your search term here" class="searchbox" />
<input type="submit" name="search" title="Search Now! "value="Search" class="searchbutton" />
</form>

我想在结果中突出显示搜索文本。

非常感谢

form.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<?php

// include MySQL-processing classes

require_once 'mysql.php'; 

try{

// connect to MySQL

$db=new MySQL(array('host'=>'','user'=>'','password'=>'','database'=>''));

$searchterm=$db->escapeString($_GET['searchterm']);

$result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ");

if(!$result->countRows()){

echo '<div class="maincontainer"><h2>No results were found. Go back and try a new search.</h2></div>'."n";

}

else{

// display search results

echo '<div class="maincontainer"><h2>Your search criteria
returned '.$result->countRows().' results.</h2>'."n";

while($row=$result->fetchRow()){

echo '<div class="rowcontainer"><p><strong>Book Id:
</strong>'.$row['BookId'].'<p><p><strong>Page Id:
</strong>'.$row['PageId'].'</p><p><strong>Page Text:
</strong>'.$row['PageText'].'</p></div>'."n"; 

}

}

echo '</div>';

}

catch(Exception $e){

echo $e->getMessage();

exit();

}

?>
</body>
</html>

【问题讨论】:

    标签: php mysql search highlight


    【解决方案1】:

    试试这个:

    $searchvalue = implode('<span style="color:green;font-weight:800;">'.$_GET['searchterm'].'</span>',explode($_GET['searchterm'],$row['PageText']));
    echo '<div class="rowcontainer"><p><strong>Book Id:
    </strong>'.$row['BookId'].'<p><p><strong>Page Id:
    </strong>'.$row['PageId'].'</p><p><strong>Page Text:
    </strong>'.$searchvalue.'</p></div>'."n"; 
    

    【讨论】:

      【解决方案2】:

      您可以使用此代码:

      <?php
       //$keyword -> keyword to be search in string.
       //$field   -> string contain value to be seach
       // Returns HTML content with highlighted text
      function makeHighlighter($keyword, $field) {
          $i = strripos($field, $keyword);
          if ($i !== false) {
              $keyword = str_ireplace($keyword, substr($field, $i, (strlen($keyword))), $keyword);
          } else {
              return $field;
          }
          $as_unm_split = explode($keyword, $field);
          $string_hig = "";
          for ($i = 0; $i < count($as_unm_split); $i++) {
              if ($i < count($as_unm_split) - 1)
                  $string_hig.=$as_unm_split[$i] . "<span style=\"color: red\">" . $keyword . "</span>";
              else
                  $string_hig.=$as_unm_split[$i];
          }
          unset($as_unm_split, $keyword, $i);
          return $string_hig;
      }
      
      echo makeHighlighter("keyword","this is keyword to be match...");
      ?>
      

      O/P

      这是要匹配的关键字...
      [注意:“关键字”将以红色显示]

      【讨论】:

      • 它不适用于 utf-8 字符。例如qué。我该如何改进它以使其正常工作?
      猜你喜欢
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多