Page 1 of 1
Preg replace to undo html tag?
Posted: Tue May 26, 2009 12:11 am
by anivad
For a comment script I used the code:
Code: Select all
$comment = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $comment);
to make all HTML links clickable; however, when someone edits that comment, the to-be-edited text then displays links as
<a href="http://site.com">site</a>,
which upon edit then becomes
<a href="<a href="http://site.com">"<a href="http://site.com"></a>
How do I replace links in the to-be-edited text with their raw, unclickable form? (i.e.
http://site.com). I'm not very familiar with preg_replace.
Thanks!
Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 3:28 am
by prometheuzz
So, what you want to replace are links that are not surrounded by quotes. You can accomplish this by using negative-lookbehind and -lookahead:
Code: Select all
'@(?<!")...your regex here...(?!")@'
Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 3:31 am
by anivad
Sorry; could you elaborate? I'm still quite new to PHP.

Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 3:38 am
by prometheuzz
anivad wrote:Sorry; could you elaborate? I'm still quite new to PHP.

You just need to add
in front of your existing regex and
at the end. That's all there is to it. If you want to know what these things actually do, then I recommend doing a search on the the internet with the keywords "negative look ahead" or "negative look behind".
Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 3:47 am
by anivad
Changed it to
preg_replace('@(?<!")https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)(?!")@', '<a href="$1">$1</a>',
and got:
Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 57
Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 3:51 am
by prometheuzz
anivad wrote:Changed it to
preg_replace('@(?<!")https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)(?!")@', '<a href="$1">$1</a>',
and got:
Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 57
That's a pretty self explanatory error message. What exactly don't you understand about it?
You obviously didn't use the (more or less) working original regex from your first post. You left something out (maybe a parenthesis?).
Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 4:00 am
by anivad
Left out a parenthesis. :\ Okay, I fixed that:
preg_replace('@(?<!")(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)(?!")@', '<a href="$1">$1</a>',
And it works halfway; now I get this instead:
<a href="http://site.com">http://site.com</a> (with the middle portion a clickable link), which means it did ignore the bit between the inverted commas.
How do I completely strip the rest of the <a href> tag?
Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 4:06 am
by prometheuzz
Not sure if I understand. You say you have the following output:
Code: Select all
<a href="http://site.com">http://site.com</a>
but what is the input? What is the desired output? Do you understand how these look-ahead-things work? (preferably, I'd like an answer to all three questions).
Thanks.
Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 4:10 am
by anivad
1. Input:
http://site.com - (1)
OR <a href="http://site.com">http://site.com</a> - (2)
2. Desired output if (1): <a href="http://site.com">http://site.com</a> - works
Desired output if (2): <a href="http://site.com">http://site.com</a> - doesn't work
Current output for (2): <a href=\"<a href="
http://site.com/">http://site.com</a>\"><a href="http://site.com">http://site.com</a>
3. No.
Thanks!
Re: Preg replace to undo html tag?
Posted: Tue May 26, 2009 4:21 am
by prometheuzz
anivad wrote:...
3. No.
Thanks!
Okay, now is the time to learn: I gave you a start, you try to finish it.
This site explains in great detail have these things work.
http://www.regular-expressions.info/lookaround.html
Feel free to post back if you get stuck after trying it yourself.