1) Instead of search in MySQL database
such as
SELECT * FROM tutorial WHERE MATCH(title,description) AGAINST ('left right' IN NATURAL LANGUAGE MODE);
I design search algorithm in PHP
2) Search box
<form method="post" action="">
<input class="" name="search" type="text" placeholder=""
value="<?php echo htmlspecialchars($_POST["search"]); ?>" /> <input id="Search_button" value="Search" type="submit">
</form>
3) Replace search word with highlight background red text white , search title and content
$pattern = htmlspecialchars($_POST["search"]);
$replacement = '<span style="background-color:red;color:white;">'.$pattern.'</span>';
$entry['title'] = eregi_replace($pattern, $replacement, $entry['title']);
4) Using function context_find($haystack, $needle, $context)
to show searching Word with 10 surrounding Words in first occurance, add link "Read More"
add '&search='.$_POST["search"] in link. When the link is clicked, highlight of search word shown in the text.
5) Example code index.php
<div id="userGuide_list">
<ul class="paginatedList nav nav-list instlist" style='padding:10px 5px;' >
<?php
$j=0;
foreach($categories as $row){
$cat_each = '<li class="'.$row['NameAbb'].'"><h5 class="strong section">'.$row['Name'].'</h5><ul class="sub-list">';
$category = $UserManualController->loadTableOfContents($row['NameAbb']);
$item = "";
$i==0;
$extra = "";
foreach($category as $entry){
if(trim($_POST["search"])!=""){
$pattern = htmlspecialchars($_POST["search"]);
if (strpos($entry['title'], $pattern) !== false) $i= $i + 1;
$replacement = '<span style="background-color:red;color:white;">'.$pattern.'</span>';
$entry['title'] = eregi_replace($pattern, $replacement, $entry['title']);
$loadContent = $UserManualController->loadContent($entry['content_id']);
$extra = $UserManualController->context_find($loadContent['content'], $pattern, 10);
$extra = eregi_replace($pattern, $replacement, strip_tags($extra));
if(!empty($extra))
$extra ='<div>... '.$extra.' ...<a href="'.SITE_BASE_URL.'/TRACSManual/tracs-manual-content.php?id='.$entry['content_id'].'&search='.$_POST["search"].'"> Read More ...</a></div>';
}
if(trim($_POST["search"])=="" || strpos($entry['title'], $pattern) !== false|| strpos($loadContent['content'], $pattern) !== false)
$item .= '<li><a href="'.SITE_BASE_URL.'/TRACSManual/tracs-manual-content.php?id='.$entry['content_id'].'&search='.$_POST["search"].'">'.$entry['title'].'</a>'.$extra;
}
$item .= '</ul></li>';
if(trim($_POST["search"])=="" || $item != '</ul></li>' ||$extra!="" ){
echo $cat_each.$item;
$j=$j+1;
}
}
if($j==0) echo "No match found.";
?>
</ul>
</div>
6. Example code of search ten words
(https://stackoverflow.com/questions/22762797/php-show-searching-word-with-5-surrounding-words)
function context_find($haystack, $needle, $context) {
$haystack=' '.$haystack.' ';
if ($i=strpos($haystack, $needle)) {
$start=$i;
$end=$i;
$spaces=0;
while ($spaces < ((int) $context/2) && $start > 0) {
$start--;
if (substr($haystack, $start, 1) == ' ') {
$spaces++;
}
}
while ($spaces < ($context +1) && $end < strlen($haystack)) {
$end++;
if (substr($haystack,$end,1) == ' ') {
$spaces++;
}
}
while ($spaces < ($context +1) && $start > 0) {
$start--;
if (substr($haystack, $start, 1) == ' ') {
$spaces++;
}
}
return(trim(substr($haystack, $start, ($end - $start))));
} else {
return false;
}
}
No comments:
Post a Comment