I have a search form, in which a user enters a keyword and it displays the results with the keyword in description as highlighted text, for highlighting, i have used another class on that keyword. problem is that, if there is a keyword in some hyperlink or in some <img> tag, it also applies that class there that causes the image load failure and links are not working.

That is how i am fetching my results and highlighting them if there is any keyword.

<?php 
if($_GET['key']){ 
        $key = $_REQUEST['key'];
$sql = "select * from tbl_feed WHERE title LIKE '%$key%' OR description like '%$key%'";
$rs  = $dbFun->selectMultiRecords($sql);
for($j=0;$j<count($rs);$j++){
        $title= mb_convert_encoding($rs[$j]['title'], "iso-8859-1","auto");
        $desc   = mb_convert_encoding($rs[$j]['description'],"iso-8859-1","auto");
        ?>
        <?php
      for($i = 0; $i < sizeof($arrKeywords); $i++){
                        $title = str_ireplace($arrKeywords[$i], "<font class=\"highlight\">" . $arrKeywords[$i] . "</font>", $title);
                        $desc = str_ireplace($arrKeywords[$i], "<font class=\"highlight\">" . $arrKeywords[$i] . "</font>", $desc);
                    }
      ?>

<a href="<?=$rs[$j]['link']?>" target="_blank"><strong><?=$title?></strong></a><BR />
<?=$rs[$j]['pubdate']?><BR /><BR />
<?=$desc?><BR /><BR /><BR />
<? }
}

is there any way that it skips the html tags from replacing?

Member Avatar for Zagga

Hi baig772,

I'm not sure if I have this the right way round.
Could you not create a copy of the original $title and $desc before the highlighting is applied, and use this copy in the links? Or apply the highlighting after you create the links?

Zagga
p.s. I hope you are sanitising $key :)

i am applying the highlighting on all over the text in which html tags are also there

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.