sKunKbad Posted April 5, 2008 Share Posted April 5, 2008 I'm making a search script that is not database related. When a match is found for the search term, I'd like to have a listing such as: PAGE TITLE ...this is the neighboring text located near the search term. this is more text, so that people can see it in context... I have the script to the point where I can list the page titles of pages that the search term occurs in, but I'm not quite sure how to go about extracting some of the neighboring text so that people can see it in context. Any suggestions? I don't want to turn my site, which is small, into a database driven site just for search capabilites. I already have a yahoo search on the site, but would like to try to build my own. This is what I have so far: <?php //a search term for testing purposes - later to be imported via POST and validated/filtered $searchPost = "Brian's"; $searchPost = trim($searchPost); $searchText = preg_quote($searchPost); echo "$searchText<br />"; //the following code makes an array of file names to search from the menuList.inc.php require "menuList.inc.php"; preg_match_all("|href=\".*\"|",$menu, $matches); $pageList = array("index.php"); for ($i=1;$i<count($matches[0]);$i++){ $match = preg_split("|\"|",$matches[0]["$i"]); $pageList[] = $match[1]; } //This is the list of files that will be searched echo "<pre>"; print_r($pageList); echo "</pre>"; //search each file from the $pageList array, and determine if the $searchText is present foreach($pageList as $page){ $handle = fopen("$page", "r"); $contents = fread($handle, filesize($page)); $text = explode("<!-- SEARCH BOUNDARY -->",$contents); $stripped_text = strip_tags($text[1]); if(preg_match("|\b$searchText\b|i",$stripped_text)){ $found[] = $page; } } //this is the array of files in which the search term was found. echo "<pre>"; print_r($found); echo "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/99763-match-extra-chars-for-search-output/ Share on other sites More sharing options...
sKunKbad Posted April 6, 2008 Author Share Posted April 6, 2008 Once again, I figured out my solution. I modified an existing script that I found, and it works perfectly for me. Here is my script in case anyone should need something similar in the future: <?php $min_chars = "3"; // Min. chars that must be entered to perform the search $max_chars = "30"; // Max. chars that can be submited to perform the search $default_val = "Search"; // Default value in searchfield $limit_hits = array("5","10","25","50","100"); // How many hits should be displayed, to suppress the select-menu simply use one value in the array --> array("100") $message_1 = "Invalid Search!"; // Invalid searchterm $message_2 = "Please enter at least $min_chars characters, but not more than $max_chars characters."; // Invalid searchterm long ($min_chars/$max_chars) $message_3= "Your search results for:"; // Headline searchresults $message_4 = "Your search had no results. "; // No hits $message_5 = "results"; // Hits $message_6 = "Match case"; // Match case $no_title = "Untitled"; // This should be displayed if no title or empty title is found in file $limit_extracts = ""; // How many extratcts per file do you like to display. Default: "" --> every extract, alternative: 'integer' e.g. "3" $byte_size = "51200"; // How many bytes per file should be searched? Reduce to increase speed //ini_set("error_reporting", "2047"); // Debugger function search_headline($_POST, $message_3) { //This function simply creates the h1 header that says, "Your search results for: '.........' @$keyword=$_POST['keyword']; @$action=$_POST['action']; if($action == "SEARCH") echo "<h2 class=\"firstContent\" style=\"border:none; border-bottom:1px dashed #C3D2DC; padding-bottom:5px;\">$message_3 '<i>".htmlentities(stripslashes($keyword))."</i>'</h2>"; } function search_error($_POST, $min_chars, $max_chars, $message_1, $message_2, $limit_hits) { //This function checks that the search was of valid character length, and validates the limit specified by the form. global $_POST; @$keyword=$_POST['keyword']; @$action=$_POST['action']; @$limit=$_POST['limit']; if($action == "SEARCH") { if(strlen($keyword)<$min_chars||strlen($keyword)>$max_chars||!in_array ($limit, $limit_hits)) { echo "<p><b>$message_1</b><br />$message_2</p>"; $_POST['action'] = "ERROR"; } } } function search_dir($message_1, $message_2, $no_title, $limit_extracts, $byte_size, $_POST) { global $count_hits; @$keyword=$_POST['keyword']; @$action=$_POST['action']; @$limit=$_POST['limit']; @$case=$_POST['case']; if($action == "SEARCH") { require "menuList.inc.php"; preg_match_all("|href=\".*\"|",$menu, $matches); $pageList = array("index.php"); for ($i=1;$i<count($matches[0]);$i++){ $match = preg_split("|\"|",$matches[0]["$i"]); $pageList[] = $match[1]; } if($count_hits>=$limit) { //and stop if the limit of hits has already been reached. break; } foreach($pageList as $page){ $fd = fopen("$page", "r"); $contents = fread($fd, $byte_size); $chopped = explode("<!-- SEARCH BOUNDARY -->",$contents); $text = strip_tags($chopped[1]); $keyword_html = htmlentities($keyword); if($case) { // if match case option was selected then apply case sensitive matching or not $do=strstr($text, $keyword)||strstr($text, $keyword_html); } else { $do=stristr($text, $keyword)||stristr($text, $keyword_html); } if($do) { //if a match was found $count_hits++; // add one to the count if(preg_match_all("=<title[^>]*>(.*)</title>=siU", $contents, $titel)) { // if there is a title add the title to the $titel array if(!$titel[1][0]) $link_title=$no_title; else $link_title=$titel[1][0]; } else { $link_title=$no_title; } echo "<p class=\"itemP\"><a href=\"$page\" class=\"result\">$count_hits. $link_title</a><br />"; // Creates a link to the file $auszug = strip_tags($text); $keyword = preg_quote($keyword); $keyword = str_replace("/","\/","$keyword"); $keyword_html = preg_quote($keyword_html); $keyword_html = str_replace("/","\/","$keyword_html"); echo "<span class=\"extract\">"; if(preg_match_all("/((\s\S*){0,3})($keyword|$keyword_html)((\s?\S*){0,3})/i", $auszug, $match, PREG_SET_ORDER)); { if(!$limit_extracts) $number=count($match); else $number=$limit_extracts; for ($h=0;$h<$number;$h++) { if (!empty($match[$h][3])) printf("<i><b>..</b> %s<b>%s</b>%s <b>..</b></i>", $match[$h][1], $match[$h][3], $match[$h][4]); } } echo "</span></p>"; flush(); } fclose($fd); } @closedir($handle); } } function search_no_hits($_POST, $count_hits, $message_4) { @$action=$_POST['action']; if($action == "SEARCH" && $count_hits<1) echo "<p class=\"result\">$message_4</p>"; } search_headline($_POST, $message_3); search_error($_POST, $min_chars, $max_chars, $message_1, $message_2, $limit_hits); search_dir($message_1, $message_2, $no_title, $limit_extracts, $byte_size, $_POST); search_no_hits($_POST, $count_hits, $message_4); ?> Quote Link to comment https://forums.phpfreaks.com/topic/99763-match-extra-chars-for-search-output/#findComment-510707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.