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

asked on

ignore url tag

hello there,
I am using preg_replace to add code tags to links and its working perfectly fine..
if there is a [img] tag it will ignore and wont add a code tag to the [img]..
I would like an addition to it.. something to ignore if it contains a [url] tag just like img..
how can I do that with the current code I have?
$post = preg_replace("/(?<!\img])(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/i","[code]\\0[/code]",$post);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America 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
Avatar of XK8ER

ASKER

I get this for some reason..

Warning: preg_replace() [function.preg-replace]:
Compilation failed: lookbehind assertion is not fixed length at offset
Building on kaufmed's answer, I've fixed a mistake in it, I think:
$post = preg_replace("/(?<!(?:img|url)\])(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/i","
\\0

Open in new window

",$post);
Sorry, reposted as code:
$post = preg_replace("/(?<!(?:img|url)\])(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/i","[code]\\0[/code]",$post);

Open in new window

And just in case that doesn't work, I'd usually use $0 instead of \\0 (which may work, but might need to be \0 maybe?):
$post = preg_replace("/(?<!(?:img|url)\])(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/i","[code]$0[/code]",$post);

Open in new window

> which may work, but might need to be \0 maybe
Disregard that - after thinking about it, I think they both \\0 and \0 should probably work, as well as $0
Avatar of XK8ER

ASKER

it works really good for this
[URL]http://google.com/image.jpg[/URL]

but for this one it wont work..
[URL=http://google.com/image.jpg][IMG]http://google.com/image.jpg[/IMG][/URL]

Open in new window

SOLUTION
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
Avatar of XK8ER

ASKER

amazing.. thanks so much!!