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

PHPRegular ExpressionsWeb Languages and Standards

Avatar of undefined
Last Comment
XK8ER

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
XK8ER

ASKER
I get this for some reason..

Warning: preg_replace() [function.preg-replace]:
Compilation failed: lookbehind assertion is not fixed length at offset
Terry Woods

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);
Terry Woods

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Terry Woods

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

Terry Woods

> 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
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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Terry Woods

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
XK8ER

ASKER
amazing.. thanks so much!!