php - PHP 在 $result = $db->query($query); 中使用 str_ireplace 在结果中突出显示搜索到的单词;
问题描述
我已经阅读了以前的帖子,但我没有找到解决方案。在行 $result = $db->query($query); 我想突出显示查询“关键字”的文本
我试过 str_ireplace 没有成功。
$aKeyword = explode(" ", $_POST['keyword']);
$query ="SELECT * FROM annullamenti WHERE testo OR title OR citta like '%" . $aKeyword[0] . "%'";
for($i = 1; $i < count($aKeyword); $i++) {
if(!empty($aKeyword[$i])) {
$query .= " AND testo like '%" . $aKeyword[$i] . "%'";
}
}
$result = $db->query($query);
if(mysqli_num_rows($result) > 0) {
$row_count=0;
While($row = $result->fetch_assoc()) {
$row_count++;
解决方案
您可能采用的一种方法是preg_replace
在遍历所有提供的关键字时生成必要的模式和替换数组。以下使用该方法并将其与动态生成的 sql 查询相结合,该查询作为prepared statement
提示执行 - 此处向@Nigel-Ren 提供有关splat
运算符的提示!
不知道annullamenti
表中保存了哪些数据,很难知道严格查询是否会返回必要的值,但OR
在测试中使用连接替代子句。
<!DOCTYPE html>
<html>
<head>
<title>search and replace - highlight words</title>
<style>
span{color:red;background:yellow}
</style>
</head>
<body>
<form method='post'>
<input type='text' name='keyword' />
<input type='submit' />
</form>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
/* include database connection */
require 'db.php';
/*
join the alternative conditions using this phrase.
Using `AND` will make the query very strict as ALL conditions must be true ( as is the case in the original sql )
whereas using `OR` will make it more relaxed... for testing I opted for `OR`
*/
$joiner=' and ';
/* types placeholder */
$types='';
/* params placeholder array */
$params=array();
/* clauses */
$clauses=array();
/* regExp patterns array */
$pttns=array();
/* replacement phrases - highlighted words */
$replace=array();
if( !empty( $_POST['keyword'] ) ){
$keywords=explode( ' ', $_POST['keyword'] );
$word=$keywords[0];
$sql='select * from `annullamenti`';
/* add clauses for each of the initial conditions */
$clauses[]='( testo like ? or title like ? or citta like ? )';
$params[]="%{$word}%";
$params[]="%{$word}%";
$params[]="%{$word}%";
/* add first pattern */
$pttns[]=sprintf( '/%s/', $word );
/* add first replacement */
$replace[]=sprintf( '<span>%s</span>', $word );
/* process any other keywords given */
for( $i=1; $i < count( $keywords ); $i++ ){
$word=$keywords[ $i ];
$clauses[]='testo like ?';
$params[]="%{$word}%";
$pttns[]=sprintf( '/%s/', $word );
$replace[]=sprintf( '<span>%s</span>', $word );
}
/* determine the `types` string to match the number of parameters */
$types=str_repeat('s', count( $params ) );
/* generate the final dynamic SQL statement */
$sql=sprintf('%s where %s', $sql, implode( $joiner , $clauses ) );
/* create the prepared statement */
$stmt=$db->prepare( $sql );
if( $stmt ){
/*
bind the types and the params to the statement using the `splat` operator (...)
*/
$stmt->bind_param( $types, ...$params );
/* run the query */
$result = $stmt->execute();
if( $result ){
$result=$stmt->get_result();
while( $rs=$result->fetch_object() ){
/* create the output string that is to have keywords highlighted */
$data = sprintf( "testo:%s, title:%s, citta:%s\n", $rs->testo, $rs->title, $rs->citta );
/* perform the regExp search and replace to create the highlighted string */
$modified = preg_replace( $pttns, $replace, $data );
/* output the highlighted string */
echo nl2br( $modified );
}
}else{
echo 'No results';
}
} else {
echo 'Bad foo - statement failed!';
}
}
}
?>
</body>
</html>
推荐阅读
- c# - 是否可以进行动态向下转换?
- firebase - 登录时出现一些错误 PlatformException(ERROR_ADMIN_RESTRICTED_OPERATION, 此操作仅限管理员使用。, null)
- javascript - 如何使用 InlineKeyboardButton 应用“Markdown 样式进行回复”?
- ios - 我可以嵌入导航控制器视图以占用自定义 UIViewController 的部分屏幕吗?
- json - 使用 bash 和 jq 以特定格式将新元素附加到 JSON 对象
- python - 如何使用 Python 获取目录中的最新文件夹
- identityserver4 - Oidc 客户端 js,不要在客户端使用 Date.now
- json - 邮递员中 json 模式的 ajv 验证是错误的
- r - R url.exists 为不存在的 URL 返回 True
- ruby - Ruby:运行 rake 任务以执行 ruby 脚本