preg_replace Help

Discussion in 'PHP' started by hrock, Jan 3, 2008.

  1. #1
    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); 
    Code (markup):
    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
     
    hrock, Jan 3, 2008 IP
  2. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Modify this as needed.

    
    $String="<a href='blah.com'>welcome to blah</a>";
        $msgStrip = preg_replace('/<a\s+.*?[href=]["|\']([^"\']+)["|\']>
    {1}([^<]+)<\/a>/is', '\2 (\1)',$String);
    
    it will output welcome to blah (blah.com)
    
    Code (markup):
    If you like what I have posted feel free to drop me some reputation =)

    Hope this helps and Happy New year
     
    LittleJonSupportSite, Jan 3, 2008 IP
  3. pratham

    pratham Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    preg_replace ('`[^"\'](keywords?)[^"\']`si', '<a href="../keyword.html">$1</a>');

    This looks for quotes (single or double) before and after the keyword(s) and does not match if there are.

    preg_replace ('`[^-](keywords?)`si', '<a href="../keyword.html">$1</a>');
     
    pratham, Jan 4, 2008 IP