Skip to content

Instantly share code, notes, and snippets.

@jesselau76
Last active October 7, 2018 02:37
Show Gist options
  • Save jesselau76/7e46356242df305eab249accf73fa43e to your computer and use it in GitHub Desktop.
Save jesselau76/7e46356242df305eab249accf73fa43e to your computer and use it in GitHub Desktop.
<?php
$q = $_GET['q'];
$sort = $_GET['sort'];
$manticore = new mysqli("127.0.0.1", "", "", "", 9306);
$q=$manticore->real_escape_string($q);
if ($sort=="1")
{
$sphinxQl="SELECT * ,SNIPPET(body,'".$q."','limit=100') as snippet FROM idx_blog WHERE MATCH('".$q."')";
}
else
{
$sphinxQl="SELECT * ,SNIPPET(body,'".$q."','limit=100') as snippet FROM idx_blog WHERE MATCH('".$q."') ORDER BY date_added DESC";
}
$result = $manticore->query($sphinxQl);
echo "Manticore search results: <BR>";
foreach($result as $row)
{
echo "<b>".highlight_word($row['title'],$q)."</b>".PHP_EOL."<BR>";
echo highlight_word($row['snippet'],$q) .PHP_EOL."<BR><BR>";
}
function highlight_word( $content, $word) {
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
$replace = '<span style="background-color: #FF0;">' . $word . '</span>'; // create replacement
$content = str_ireplace( $word, $replace, $content ); // replace content
return $content; // return highlighted data
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment