2017-07-11 16 views
-1

すべての$ searchkey結果を強調表示したいと思います。 他のソリューションを試しましたが、検索エンジンでは機能しません。 結果が表に表示されます。 キーワードの外観をすべて強調表示したい。PHPハイライト入力検索キーワード

ありがとうございました!

<?php 
$dbhost = 'xxx'; 
$username = 'xxx'; 
$password = 'xxx'; 

mysql_connect("$dbhost" , "$username", "$password"); 
mysql_select_db("xxx") or die("could not find the database!"); 
$output = ''; 


if(isset($_GET['search'])) 
{ 
    $searchkey = $_GET['search']; 
    $query = mysql_query("SELECT * FROM xxx WHERE email LIKE '%$searchkey%' ") or die("Could not search"); 
    $count = mysql_num_rows($query); 

    if ($count == 0) 
    { 
     $output = 'There was no search results !' ; 
    } 
    else 
    { 
    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr><th>email</th><th>hashkey</th></tr>"; 

     while ($row = mysql_fetch_array($query)) 
     { 

      $email = $row['email']; 
      $hashkey = $row['hashkey']; 
      echo "<tr><td>"; 
      echo $email; 
      echo "</td><td>"; 
      echo $hashkey; 
      echo "</td></tr>"; 
     } 
      echo '<div>'."$count results were found for '$searchkey'.".'</div>'.'</br>'; 

    } 
    echo "</table>"; 
    $searchkey= $words; 
} 
?> 

答えて

1

これを行うには、PHPで正規表現を使用することができます。このコードでは<strong>タグを使用していますが、必要に応じて置き換えることができます。

while ($row = mysql_fetch_array($query)) 
{ 

    $email = preg_replace('/(' . $searchkey . ')/s', '<strong>\1</strong>', $row["email"]); 
    $hashkey = $row['hashkey']; 
    echo "<tr><td>"; 
    echo $email; 
    echo "</td><td>"; 
    echo $hashkey; 
    echo "</td></tr>"; 
} 
+0

ありがとうございます! "i"は大文字と小文字を区別したくないので、どこに追加しますか? – alb

+0

'i'を使用するとパターンの大文字小文字が区別されます。 'preg_replace( '/ $ searchkey。')/ is '、' \ 1 '、$ row [" email "]);' –

関連する問題