I'm using a regex to find any URLs and link them accordingly. However, I do not want to linkify any URLs that are already linked so I'm using lookbehind to see if the URL has an href before it. This fails though because variable length quantifiers aren't allowed in lookahead and lookbehind for PHP.
Here's the regex for the match:
/\b(?<!href\s*=\s*[\'\"])((?:http:\/\/|www\.)\S*?)(?=\s|$)/i
What's the best way around this problem?
EDIT:
I have yet to test it, but I think the trick to doing it in a single regex is using conditional expressions within the regex, which is supported by PCRE. It would look something like this:
/(href\s*=\s*[\'\"])?(?(1)^|)((?:http:\/\/|www\.)\w[\w\d\.\/]*)(?=\s|$)/i
The key point is that if the href is captured, the match is immediately thrown out due to the conditional (?(1)^|)
, which is guaranteed to not match.
There's probably something wrong with it. I'll test it out tomorrow.