Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

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="http://google1.com"]google is a search engine[/URL]
[URL=http://google2.com]google is a search engine[/URL]
[URL=http://googleimg.com][IMG]http://google.com/logo.jpg[/IMG][/URL]
 
into
 
http://google1.com
http://google2.com
http://googleimg.com
 
 
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

Avatar of fcardinaux
fcardinaux
Flag of Switzerland image

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:

<?php
 
$posts = array(
    '[URL="http://google1.com"]google is a search engine[/URL]',
    '[URL=http://google2.com]google is a search engine[/URL]',
    '[URL=http://googleimg.com][IMG]http://google.com/logo.jpg[/IMG][/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

ASKER CERTIFIED SOLUTION
Avatar of fcardinaux
fcardinaux
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial