2010-04-30 12 views
0

J'essaie de comprendre ce qui ne va pas dans ce code. il ne met pas en surbrillance le résultat de la recherche OU il affiche les balises html entourant le texte en surbrillance. .mettre en évidence les résultats de recherche dans l'erreur php

$search_result = ""; 
$search_result = trim($search_result); 

$special_cases = array('%', '_', '+'); 
$search_result = str_replace($special_cases, '', $_GET["q"]); 


//Check if the string is empty 
if ($search_result == "") { 
    echo "<p>Search Error</p><p>Please enter a search...</p>" ; 
    exit(); 
     } 

$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . mysql_real_escape_string($search_result) .'%" ORDER BY idQuotes DESC', $conn) 
    or die ('Error: '.mysql_error()); 

//eliminating special characters 
function h($s) { 
    echo htmlspecialchars($s, ENT_QUOTES); 
} 

function highlightWords($string, $word) 
{ 

     $string = str_replace($word, "<span style='background-color: #FFE066;font-weight:bold;'>".$word."</span>", $string); 
    /*** return the highlighted string ***/ 
    return $string; 

} 

?> 

<div class="caption">Search Results</div> 
<div class="center_div"> 
<table> 
    <?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) { 
     $cQuote = highlightWords($row['cQuotes'], $search_result); 
     ?> 
     <tr> 
     <td style="text-align:right; font-size:15px;"><?php h($row['cArabic']); ?></td> 
      <td style="font-size:16px;"><?php h($cQuote); ?></td> 
      <td style="font-size:12px;"><?php h($row['vAuthor']); ?></td> 
      <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td> 
     </tr> 
    <?php } ?> 
</table> 
</div> 

sur le navigateur, il est émis comme:

A good <span style='background-color: #FFE066;font-weight:bold;'>action</span> is an ever-remaining store and a pure yield 

ou si un div est utilisé avec la classe:

A good <div class='highlight'>action</div> is an ever-remaining store and a pure yield 
+0

Quel est le problème? –

+0

Les balises html sont affichées en sortie, au lieu de les mettre en forme et de mettre en surbrillance le mot-clé. – input

Répondre

1

Votre fonction de sortie h() échappe tous les caractères html (htmlspecialchars)

Modifier:

$cQuote = highlightWords($row['cQuotes'], $search_result); 

Pour:

$cQuote = highlightWords(htmlspecialchars($row['cQuotes']), $search_result); 

Et le changement:

<td style="font-size:16px;"><?php h($cQuote); ?></td> 

Pour:

<td style="font-size:16px;"><?php echo $cQuote ?></td> 
+0

merci beaucoup !!!! – input

+0

De rien. Heureux d'avoir pu aider. – webbiedave