preg replace for links

hello there,
I have a little function for links to replace them from a string and its working fine..
I would like to add something so that if there is an image inside the link to ignore the replace
for example this code does this

$post = preg_replace('/\[url="?([^\]"]+)[\]"].*/i', '$1', $post);
[URL=""]google is a search engine[/URL]
[URL=]google is a search engine[/URL]
im trying to do something so if there is [img] tags inside the [url] to NOT do a replace...
how can I do that?

Open in new window

Who is Participating?
fcardinauxConnect With a Mentor Commented:
In the other situation, you need to start by transforming the image links into a different, unmatchable pattern, then to replace the URLs, and finally to transform the image links back to their original pattern.
$post = <<<ENDOFPOST
Everybody knows that [URL=""]google dot com[/URL] is a search engine, and 
that [URL=]yahoo dot com[/URL] is a search engine as well. Here is google's
logo: [URL=][IMG][/IMG][/URL]. Enjoy!
$post = preg_replace('/\[url="?([^\]"]+)"?\]\[img\]([^\[]*)\[\/img\]\[\/url\]/i', '{~I~$1~M~$2~G~}', $post);
$post = preg_replace('/\[url="?([^\]"]+)"?\][^\[]*\[\/url\]/i', '$1', $post);
$post = preg_replace('/\{~I~([^~]*)~M~([^~]*)~G~\}/i', '[URL="$1"][IMG]$2[/IMG][/URL]', $post);
echo "$post<br>\n";

Open in new window

Is $post a text where there can be any number of links ?

If not, if each $post only consists in one link, you can do this:

$posts = array(
    '[URL=""]google is a search engine[/URL]',
    '[URL=]google is a search engine[/URL]',
foreach ($posts as $post) {
    if (false === stripos($post, '[img]')) {
        $post = preg_replace('/\[url="?([^\]"]+)[\]"].*/i', '$1', $post);
    echo "$post<br>\n";

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.