Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    preg_replace that ignores content inside a, img and other specified tags

    I am creating automated mouseover tooltips for definitions of words in a glossary database. The problem is that I can't figure out how to not replace the word when it appears in hyperlinks, img and various heading tags.

    I have populated an array with all the words and their definitions - $glossary

    What I have at the moment is a simple str_replace, because I actually have no idea how to start this preg_replace.

    PHP Code:
        foreach($glossary as $glossary_word => $glossary_definition){
            
    $content str_replace($glossary_word,'<a href="javascript:;" style="cursor:help;color:#BF601A;" onmouseover="Tip(\''.$glossary_definition.'\')" onmouseout="UnTip()">'.$glossary_word.'</a>',$content);
        } 

    Would be great if I could populate an array of tags that would be ignored when doing the replace ie:
    $tags_to_ignore = array('a','img','h1','h2','h3',etc) and somehow include this in the preg_replace.

    What this would mean is that the function would only add the tooltip to occurrences of the word not in these specified tags.


    Thanks for any thoughts on this.

  • #2
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    anyone?

    I was also thinking about a way to ignore tags using dom somehow, but can't quite put anything together

  • #3
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is pretty close:

    http://web.archive.org/web/200803121...blok/on/080307

    works for inside tags. but not within h1 etc tags - should be able to tweak it though.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •