首页 > 解决方案 > 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++;                

标签: php

解决方案


您可能采用的一种方法是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>

推荐阅读