I’m using preg_replace with an array to go through my bbcode and make the change when a news entry is posted.
For example
$bbcode = array (
"/\[url\=(.*?)\](.*?)\[\/url\]/is" => "<a href='$1' target='_blank'>$2</a>"
);
So then when I pull data from the database (in this example $newsPost) which contains the text and bbcode, I do this.
$newsPost = preg_replace(array_keys($bbcode), array_values($bbcode), $newsPost);
Now, what I’d like is to find out if the value of $1 contains my domain, the target should be “top” and if not, it should be blank.
So if we have [url=http://www.mydomain.com]visit our page[/url]. then it’s transformed to <a href="http://www.mydomain.com" target="top">visit our page</a> and anything else will have target="_blank".
Any ideas?
Thanks in advance!
EDITED/FIXED
str_replace()to deal with dots in domain names\\in the string) but I have undone this as you have said it was working for you in the first place.How about this?
If that will work for you, a caveat: Don’t set
$mydomaintowww.domain.tld, set it todomain.tld, so you catch all subdomains.You could even do it with multiple domains like this: