Preg replace to undo html tag?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Preg replace to undo html tag?

Post 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!
Last edited by Benjamin on Tue May 26, 2009 10:54 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Preg replace to undo html tag?

Post 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...(?!")@'
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: Preg replace to undo html tag?

Post by anivad »

Sorry; could you elaborate? I'm still quite new to PHP. :(
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Preg replace to undo html tag?

Post by prometheuzz »

anivad wrote:Sorry; could you elaborate? I'm still quite new to PHP. :(
You just need to add

Code: Select all

(?<!")
in front of your existing regex and

Code: Select all

(?!")
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".
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: Preg replace to undo html tag?

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Preg replace to undo html tag?

Post 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?).
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: Preg replace to undo html tag?

Post 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?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Preg replace to undo html tag?

Post 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.
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: Preg replace to undo html tag?

Post 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!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Preg replace to undo html tag?

Post 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.
Post Reply