Tertius Posted June 29, 2007 Share Posted June 29, 2007 i am writing highlighting function for the keywords of search engine, for example ,i type "1" in the search bar, it pops out the locations of webpages that contain "1", and then when I click the webpage. it will search all the data I choose from database at that page, for example, i want it to show some contents from database, so i type echo $array[content] something like that. those data is from database, but it will also search the html code inside the $array[content], and highlight them. for example, i have a article. <p><br><font size="12px">Abc</font> and def are are friend,they meet together at 1997.</p> this is the article in the database,this is the data inside the $array[content], and I search the "1" and replaced it with $replacement = "<font class=\"searchhighlight\">$searchterm</font>"; which is a css. its css code is like this. .searchhighlight {background-color:#99CC99; color:#000099; font-style:italic;} ,i make it this way, $arraycontent = str_replace("$searchterm","$replacement","$array[content]"); and then, i want it to replace the "1" inside the sentense, Sentense is "they meet together at 1997." the "1" inside 1997. but it will also, highlight the "1" inside the html code,so it will be like this. <p><br><font size="<font class=\"searchhighlight\">1</font>2px">Abc</font> and def are are friend,they meet together at <font class=\"searchhighlight\">1</font>997.</p> so,now i only want it to show like this, print the code of this: <p><br><font size="12px">Abc</font> and def are are friend,they meet together at <font class=\"searchhighlight\">1</font>997.</p> can you tell me how will you guys make it become this way? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 29, 2007 Share Posted June 29, 2007 try <?php function highlight_search_string ($art, $srch) { $replace = sprintf('<span class="searchhighlight">%s</span>', $srch); $newtext = ''; $len = strlen($art); $str = ''; for ($i=0; $i < $len; $i++) { switch ($c = $art{$i}) { case '<': if (strpos($str, $srch) !== false) $str = str_replace($srch, $replace,$str); $newtext .= $str; $str = $c; break; case '>': $str .= $c; $newtext .= $str; $str = ''; break; default: $str .= $c; break; } } if (strpos($str, $srch) !== false) $str = str_replace($srch, $replace,$str); $newtext .= $str; return $newtext; } /** * Call the function */ $article = '<p> <font size="12px">Abc</font> and def are are friend,they meet together at 1997.</p>'; $search = '1'; echo highlight_search_string($article, $search); ?> Quote Link to comment Share on other sites More sharing options...
Tertius Posted June 30, 2007 Author Share Posted June 30, 2007 how about if i want to write it in javascript, just let the javascript search on the the text, not the html code, not using php. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 30, 2007 Share Posted June 30, 2007 If you are not looking for a PHP solution, don't post in the PHP Help forum. Quote Link to comment 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.