0

I'm using this code to replace urls in a string

preg_replace('#<a.*?>(.*?)</a>#i', '\1', $text)

How do I do the same thing, but keeping urls that match a certain pattern (ie start with a domain I want to keep)?

Update

Turns out that the urls I want to keep include relative urls, so I now want to eliminate all urls that don't match the given url pattern and are not relative links.

0

1 Answer 1

1

You need a negative look-ahead assertion:

preg_replace('#<a(?![^>]+?href="?http://keepthisdomain.com/foo/bar"?).*?>(.*?)</a>#i', '\1', $text);

Edit: If you want to match only relative domains, the logic is the same. Just take out the protocol and domain name:

preg_replace('#<a(?![^>]+?href="?/?path/to/foo/bar"?).*?>(.*?)</a>#i', '\1', $text);

The ? after " and / means that those characters are optional. So, the second example will work for either path/to/foo/bar or /path/to/foo/bar.

Sign up to request clarification or add additional context in comments.

7 Comments

How do I get this to keep relative urls in addition to the ones matching the keep domain?
Just tweak it; see my edit. Please accept the answer if you found it helpful!
Was also wondering how to replace urls that appear in the text even if they are not hyperlinks. So a free form url would be stripped out as well. Keeping urls that match the keepthisdomain of course. I will be applying the hyperlink strip regex before the free form strip regex
@lordmj The logic here is the same. Just use the first version, without the portions relevant to anchor tags, like so: preg_replace('#(?!http://keepthisdomain.com/foo/bar)http://[^<>\s]*#i', '\1', $text); Note: this may require some tweaking, because it would apply to everything, not just plain text. So, it might nuke some img tags, for example. You would need to modify the "good" URL pattern to preserve anything you want to keep.
Revisitng this again. Now I want to remove the same hyperlinks as before. But if it has the attribute rel="shadowbox[a]" I want to keep the hyperlink.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.