php - 突出显示 Mysql 搜索。但 preg_replace 不起作用?

标签 php mysql

<h2>Search</h2> 
<form name="search" method="post" action="<?=$PHP_SELF?>">
Search for: <input type="text" name="find" />
<select name="field">
<option VALUE="name">Name</option>
<option VALUE="location">Location</option>
</select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<? 
//This is only displayed if they have submitted the form 

$find = $_POST['find'];
$field = $_POST['field'];
$searching = $_POST['searching'];

if ($searching =="yes") 
{ 
//echo "<h2>Results</h2><p>"; 

//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// Otherwise we connect to our Database 
mysql_connect("localhost", "DB_name", "Password") or die(mysql_error()); 
mysql_select_db("wine") or die(mysql_error()); 

// We preform a bit of filtering 
//$find = strtoupper($find); 
//$find = strip_tags($find); 
//$find = trim ($find); 

$find = mysql_real_escape_string($find);
echo "<br><br><br><font size=5>Searched results for:</b> " .$find; 
echo "</font><br><br>"; 
//Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * from
(select * FROM chardonnay WHERE MATCH $field AGAINST('%".$find."%' IN BOOLEAN MODE)
union 
select * from pinotnoir WHERE MATCH $field AGAINST('%".$find."%' IN BOOLEAN MODE)
union 
select * from redwines1 WHERE MATCH $field AGAINST('%".$find."%' IN BOOLEAN MODE)
union 
select * from redwines2 WHERE MATCH $field AGAINST('%".$find."%' IN BOOLEAN MODE)
union 
select * from redwines3 WHERE MATCH $field AGAINST('%".$find."%' IN BOOLEAN MODE)
union
select * from redwines4 WHERE MATCH $field AGAINST('%".$find."%' IN BOOLEAN MODE))
a order by grape, name
"); 

while($result = mysql_fetch_array( $data )) 
{ 

 //And we display the results 

$explode_criteria = explode(" ", $_GET['find']);
$highlight = $result['name']; // capture $result['name'] here
foreach ($explode_criteria as $key)
{

// escape the user input
$key2 = preg_quote($key, '/');
// keep affecting $highlight
$highlight = preg_replace( "/" .$key2. "/", "<span style='color: red'>".$key."    </span>", $highlight);


echo $highlight;
}

我正在尝试突出显示多个搜索词。我在这个网站上搜索过,但这是我能理解的最简单的代码。但不知何故它不起作用。如果我将 $_GET['find'] 更改为 $find.我得到一个关键字突出显示,但如果我搜索两个关键字,我会得到多个重复的关键字。有些突出,有些则不突出。你们能给我一些如何修复此代码的指示吗?谢谢。

最佳答案

至少,在完成所有替换之前,您不应该回显 $highlight。事实上,您每次都会回显它来搜索 $key

foreach ($explode_criteria as $key)
{
  // escape the user input
  $key2 = preg_quote($key, '/');
  // keep affecting $highlight
  $highlight = preg_replace( "/" .$key2. "/", "<span style='color: red'>".$key."    </span>", $highlight);
}    

echo $highlight;

关于php - 突出显示 Mysql 搜索。但 preg_replace 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18210531/

相关文章:

java - 记录执行的 java 代码的行号

mysql - 销售和多项结果

php - 使用 php 递归构建类别树

php - 根据位置更新数据库和查询

php - Wordpress SQL 选择错误

php - 当请求中有特殊字符时重定向到 root - laravel 5

php - Google Api PHP 客户端库

php - YouTube API:将视频上传到特定 channel

php - Mysqli 准备无法在另一个准备好的语句中工作

mysql - Confluence jdbc 连接器无法使用时间戳列进行增量查询