matthew9090 Posted March 6, 2011 Share Posted March 6, 2011 i am making a search engine to search my site and the code is going well but i don't know how to find your search within the results and put it in bold (like google does). I use a thing called http://simplehtmldom.sourceforge.net/ for getting the html of my website. <?php include('simple_html_dom.php'); ?> <?php $s = $_GET["s"]; if ($s == null) echo "you have not searched for anything"; else echo "you searched for $s<hr>"; $urls[0] = 'http://url.com/'; $urls[1] = 'http://url2.com'; foreach ($urls as $url) { $html = file_get_html($url); strip_tags($html); preg_match("/<body>(.*)<\/body>/s", $html, $body); $htmls_split = split('<', $body[1]); foreach ($htmls_split as $line) { if (preg_match("/$s.*/", $line, $matches)) { echo "<p>$matches[0]"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229740-find-a-word-within-text-and-put-it-in-bold/ Share on other sites More sharing options...
Fergal Andrews Posted March 6, 2011 Share Posted March 6, 2011 Hi matthew9090, Is the file_get_html() function definitely returning the page HTML? If it is, then you are not assigning the output of strip_tags to anything. Try: $html = strip_tags($html); split() is a deprecated function in php 5.3. Maybe try explode(). Hope that helps, Fergal Quote Link to comment https://forums.phpfreaks.com/topic/229740-find-a-word-within-text-and-put-it-in-bold/#findComment-1183457 Share on other sites More sharing options...
matthew9090 Posted March 6, 2011 Author Share Posted March 6, 2011 no it defiantly retrieves the html and displays the results i just need to hightlight the searched words within the results Quote Link to comment https://forums.phpfreaks.com/topic/229740-find-a-word-within-text-and-put-it-in-bold/#findComment-1183460 Share on other sites More sharing options...
flolam Posted March 6, 2011 Share Posted March 6, 2011 str_replace($search_keyword, "<b>{$search_keyword}</b>", $search_result_table); Quote Link to comment https://forums.phpfreaks.com/topic/229740-find-a-word-within-text-and-put-it-in-bold/#findComment-1183462 Share on other sites More sharing options...
matthew9090 Posted March 6, 2011 Author Share Posted March 6, 2011 were about do I put that in the script? Quote Link to comment https://forums.phpfreaks.com/topic/229740-find-a-word-within-text-and-put-it-in-bold/#findComment-1183463 Share on other sites More sharing options...
matthew9090 Posted March 6, 2011 Author Share Posted March 6, 2011 code so far <?php include('simple_html_dom.php'); ?> <script type="text/javascript"> function redirect(){ window.location.href = "index.html"; } </script> <?php $s = $_GET["s"]; if ($s == null) echo "you have not searched for anything" . '<script type="text/javascript">window.onload = setTimeout("redirect()", 3000);</script>'; else echo "you searched for $s<hr>"; $urls[1] = 'http://url.com/'; $urls[2] = 'http://url2.com'; foreach ($urls as $url) { $html = file_get_html($url); strip_tags($html); preg_match("/<body>(.*)<\/body>/s", $html, $body); preg_match('@^(?:http://)?([^/]+)@i', $html, $matches); $htmls_split = split('<', $body[1]); foreach ($htmls_split as $line) { if (preg_match("/$s.*/", $line, $matches)) { if (strlen($s) == 0) die(); else str_replace($s, "<b>$s</b>", $matches[0]); echo "<p>$matches[0]"; } } } ?> the str_replace doesn't do anything at the moment. :'( please help! Quote Link to comment https://forums.phpfreaks.com/topic/229740-find-a-word-within-text-and-put-it-in-bold/#findComment-1183466 Share on other sites More sharing options...
QuickOldCar Posted March 6, 2011 Share Posted March 6, 2011 I made a multiple word color highlighter for my search index. Is 2 versions in code, one is for an array of words and the other explodes lines of text into an array. The code and demo is at the link. http://get.blogdns.com/dynaindex/color-highlighter.php Quote Link to comment https://forums.phpfreaks.com/topic/229740-find-a-word-within-text-and-put-it-in-bold/#findComment-1183535 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.