Matching URLs *not* inside [code] tags

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Goldeneye
Forum Newbie
Posts: 7
Joined: Wed Dec 24, 2008 10:32 pm

Matching URLs *not* inside [code] tags

Post by Goldeneye »

In my script, URLs are automatically parsed into Anchored HTML links. I also have a [ code ] (I call it [raw] -- as in raw-text) that obviously used for when you don't want text to formatted. The problem is that URLs inside [raw] tags get automatically hyper-linked.

So, how would I match URLs not inside [ code ] tags?

I tried this (and other several modifications):

Code: Select all

'/[^(\&\#91;raw\])] ((mailto:|(http|ftp|nntp|news):\/\/).*?)(\s|<|\)|"|\\\\|\'|$) [^(\&\#91;\/raw\])]/si'
But it doesn't work.. at all.

I use [^(\&\#91;raw\])] instead of [^(\[raw\])] because I replace the opening-square bracket with it's corresponding entity to prevent it from being matched later on by my other Regex.

A preemptive thanks for your time.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Matching URLs *not* inside [code] tags

Post by ridgerunner »

This would be extremely difficult (if not downright impossible) to reliably achieve with regex alone. I would recommend first splitting out the [raw] sections, then apply the linkification regex to the stuff outside the [raw], then piece it all back together. This is what the parse_message() function in the parser.php code does for the punBB 1.2 forum software. You can download punbb-1.2.22.zip here and look at how they do it.

Good luck!
Goldeneye
Forum Newbie
Posts: 7
Joined: Wed Dec 24, 2008 10:32 pm

Re: Matching URLs *not* inside [code] tags

Post by Goldeneye »

Really? It seems like it'd be possible to do with Regular Expressions. I'll check out how PunBB does it, though. Thanks a lot! This should prove to be easier than figuring out a Regular Expression for it. Thank you, ridgerunner!
Post Reply