Goulash
Goulash

Reputation: 3838

Replacing certain <a href..certain.domain.com... with php preg_replace

So, I have a string of text:

string with a bunch of links but i need to simply get rid of these <a href="a.domain.com">Title</a> string with more links and text

I need to get rid of everything from <a to a> where a.domain.com is present (its always the same domain) using PHP preg_replace.
It seems so simple but I can't figure this out >.< or find anywhere that explains regex :/

Upvotes: 0

Views: 271

Answers (2)

Thomas Wright
Thomas Wright

Reputation: 1309

I always like this site for regexp: http://www.regular-expressions.info/ This isn't a full answer, but I would just match <a [...]>[...]</a> and make sure it's not greedy, so you don't end up tearing everything from the first occurance of <a [...]> all the way to the last </a>. This was recommended in another forum, you might want to give it a try as well:

$rev= preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $_POST['rev']);

Upvotes: 1

mdarwi
mdarwi

Reputation: 933

If you're looking to keep what's inside the <a> tags, you can try the strip_tags() function.

If you want something more complicated, great tutorial for learning regular expressions is http://www.regular-expressions.info/

You can try something like

$no_tags = preg_replace('#<a (.*?)>(.*?)</a>#', '', $text);

but note that this will break if your tags are a more complex than the example you posted.

Upvotes: 0

Related Questions