April 8th, 2010, 12:44 PM
Replacing plain text URLs with links dilemma
Okay, replacing plain text URLs with a link is pretty easy, but I've run into an issue. The way I'm replacing them, it's able to read into the HTML, kind of [BBCode class].
So if I have: http://wat.com
and <img src="http://wat.com">
It would replace both, and then I'd end up with:
<img src="<a href="http://wat.com" height="y" width="x" />
Etc. Right now I'm using
To find and replace the links, but I need to ignore it if it has src=" behind it. How can I do that? I've tried adding ^[src.*?] but it has no effect.
April 8th, 2010, 12:57 PM
If it doesn't get more complex, this might work:
A regular expression based bbcode parser isn't a good idea for all regex libraries, though. If we're talking (e.g.) PHP here, you'll probably hit a wall pretty soon.
Last edited by JClasen; April 8th, 2010 at 01:00 PM.
April 8th, 2010, 01:00 PM
What I do in situations like this is split the string into HTML and non-HTML parts (with PHP it's preg_split). Then I can apply some replacements to HTML text and other replacements to non-HTML text.
April 8th, 2010, 01:17 PM
I don't see why, but that won't work.
Originally Posted by JClasen
And I'm applying this after the BBCode replaces everything, but maybe I'll look into another way. Right now I'm matching the 'tags', listing it and then using a switch() to replace the attributes, inner text and the tags themselves.
Is that still an inefficient way to do it?
April 8th, 2010, 01:27 PM
Seems as if php's regex extension doesn't like look behind assertations with variable length. Try this instead:
For the last part:
yes, that should work better.
Comments on this post
April 8th, 2010, 01:29 PM
That it did
Awesome :-P good job