Jump to content

find a word within text and put it in bold


matthew9090

Recommended Posts

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]";
      }
   }
}

?>

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

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!

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.