2

Possible Duplicate:
PHP Dom Remove element leave contents

I'm adding a function to a WordPress theme's function.php.

I want to have it strip links from markup, something like this:

function clean_content($content) { <br>
    $content = preg_replace('some regex goes here, but what?', '', $content);
    return $content;
}

add_filter('the_content', 'clean_content', 1);

What is the regex for stripping links but keeping the rest of the markup, but only for links from certain domains?

Per example, I want to strip all links that contain google.com in the href attribute, but keep the markup and text (e.g.: <img> for image links, etc.).

Thanks, thanks, thanks!

Community
  • 1
  • 1
Tylerr
  • 39
  • 2
  • 7
  • 2
    Use DOM, not regex (unless you're really good at regex, which you probably aren't since you are asking this question). See other question linked above. – netcoder Jun 10 '11 at 13:49
  • Other tip: Use XPath to find nodes that contains specific domains in the link, like this: `//a[contains(@href, "google.com")]`, then remove those nodes. – netcoder Jun 10 '11 at 13:56
  • This is very nice, thanks, but I'd like to have the regex for future purposes. You're correct, I'm not a regex guru, that's why I'm consulting with the Stack Overflow community. ;) That is the reasons why I tagged it regex, not just php. Thanks anyway! – Tylerr Jun 10 '11 at 13:59
  • 1
    @netcoder The DOM solution is the right way to go, very elegant and is probably the way I'll go. Thank you for pointing it out. – Tylerr Jun 10 '11 at 14:06
  • Tylerr: You can find all sorts of reasons why not to do that with regex on StackOverflow. Besides, it's way easier not to and has less unexpected side-effects (none if done correctly). – netcoder Jun 10 '11 at 14:06
  • chances are with regex you will leave a way to slip in an anchor tag. <a href="">click</a> – dqhendricks Jun 10 '11 at 15:25

0 Answers0