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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.