hi,

I use Preg_replace to automatically links some keywords

Here is my code

$texte = preg_replace('`\b((keyword)s?)\b`si','<a href="../keyword.html">$1</a>',$texte); 

It workes great but when my keyword is inside an element like <a href="zzz"> </a> it breaks the links.

My questions is how can i do that preg_replace will note replace any keywords inside an element?

And a second question how can i do that preg_replace dosen't match when there is an - directly before my keyword?

I hope that you all understand my english

Thanks for your help

    i have a similar problem, so perhaps we have a similar solution. I'm attempting to highlight the results of my search.

    This code works perfectly, if i have only one search word, or two contigious words. However, if the search returns the two words separately (as often happens), then i get no result in my highlighting.

    Also if i use any advanced search features such as prepending a "+" or a "-", the the highlighting is not there.

    here is my code and hopefully someone can tell me where i'm wrong.

    $srch=mysql_real_escape_string($_POST['search']);
    if ($srch!==""){
        $sql = "select *, match(synopsis) against('$srch') as relevance from cases where match(synopsis) against('$srch' IN BOOLEAN MODE) order by relevance desc";		
        $result = mysql_query($sql);
        if(!$result){
            echo "No matches for ".$srch.".";
        }else{
            while($r = mysql_fetch_array($result)){
    	echo $r[case_id]."&nbsp;&nbsp";
    	echo '<a href="buttons.php?case_id='.$r[case_id].'&mode=add"><img src="/images/add.gif" border="0" alt="Make payments with PayPal - it\'s fast, free and secure!"></a><br>';
    	echo "<b>".$r[headline]."</b><br>";
    	$str = $r[synopsis];
    	$str =  eregi_replace($srch,'<span style="background-color:#8AB0FF">'.$srch.'</span>', $str);
    	echo $str."<br><br><br>";
                }
         }
    }
    

    thanks!

      $texte = 'This is some text do not replace this -keyword, replace this keyword do not replace this 
      other <a href="keyword.htm">keyword</a>';
      $texte = preg_replace('`\b([^-](keyword)s?)\b`si','<a href="../keyword.html">$1</a>',$texte);
      echo htmlentities($texte);
      

      Output

      This is some text do not replace this -keyword, replace this<a href="../keyword.html"> keyword</a> do not replace this other <a href="keyword.htm">keyword</a>
      

      [-] stands for "not -"; It doesn't change the link as well but I have no idea why.

        Hi wilku

        Thanks for your help

        Is it possible to remove the space in the anchor link before the keyword? <a href="zz"> keyword</a>

        I still have problem with some link that content my keyword like

        -www.keyword.com still match

        I can't use htmlentities

        For now my code is

        $message = preg_replace('`\b([^-](keyword)s?)\b`si','<a href="../keyword.html">$1</a>', $message,1);

        Thank you

          Write a Reply...