nicob Posted December 11, 2008 Share Posted December 11, 2008 I'm using this script for searching in txt or html files. The problem is that it doesn't always show the complete line. for example I search for 'france'. My test.html contains: http://www.link1.com/ website about france blablabla http://www.link2.com/ website about blabla and people in france blablabla the result I get: http://www.link1.com/ website about france blablabla link2.com/ website about blabla and people in france blablabla So if the keyword is NOT near the beginning of the line it breaks the sentence. So what do I have to modify to get always the complete line?? btw: This script is made by wildteen88 (phpfreaks mod) <?php function keywordSearch(&$keywords) { global $keyword_count; $lines = file('test.html'); $results = array(); $symbols = array('[', ']', '-', '(', ')'); $replace = array('\[', '\]', '\-', '\(', '\)'); // colors for highlighting keywords $colors = array( '#FF0000', // red '#0000FF', // blue '#99CC00', // green '#CCCC00', // yellow '#660066', // purple '#FF0099' // pink ); $i = 0; foreach($lines as $line) { $keyword_found = false; foreach($keywords as $key => $keyword) { if(!isset($keyword_count[$keyword])) { $keyword_count[$keyword] = 0; } $keyword = str_replace($symbols, $replace, $keyword); if(eregi($keyword, $line)) { $keyword_found = true; // remove answer from end of line $line = eregi_replace("\*([a-z0-9 ]+)", '?' , $line); // highlight search keyword $line = eregi_replace("($keyword)", "<span style=\"color: {$colors[$key]}; font-weight: bold;\">\\1</span>", $line); $results[$i] = $line; $keyword_count[$keyword]++; } } if($keyword_found) $i++; } return $results; } function displaySearchResults() { global $keyword_results, $keywords, $keyword_count; $output = '<p>The keywords "<i><b>'. implode('</b></i>", "<i><b>', $keywords) . '</b></i>" found ' . count($keyword_results) . " result(s):\n"; if(is_array($keyword_results) && count($keyword_results) > 0) { $output .= "<ol>\n <li>" . implode("</li>\n <li>", $keyword_results) . "</li>\n</ol>\n"; } echo "<p>$output</p>"; } if(isset($_POST['submit'])) { if(!empty($_POST['keyword'])) { $_POST['keyword'] = str_replace(array("\r\n", "\r", "\n"), "\n", $_POST['keyword']); $keywords = explode("\n", $_POST['keyword']); $keyword_results = keywordSearch($keywords); displaySearchResults(); } else { echo 'Invalid search term'; } } ?> Link to comment https://forums.phpfreaks.com/topic/136580-adjust-this-script-to-show-complete-lines/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.