tags:

views:

44

answers:

2

I have written regular expression which converts text links into clickable link. The code works fine however when an HTML tag is passed it mess the code

Here is my code

$text = preg_replace('#(?<![">])((http|ftp)s?://[^<>\s]+)#i', '<a href="\\0">\\0</a>', $text );

It is suppose to skip following element

<a href="http://www.yahoo.com"&gt;Yahoo&lt;/a&gt;

but it does not.. what am I doing wrong?

Thanks

+1  A: 

It does skip it. " is before the URL, so the negative look behind makes it not match.

Anyway, to do this reliably, you should be using a HTML parser. You could transform only text nodes not worrying about existing HTML elements.

Artefacto
Any example of HTML parser?
jason
@jas You can use `DOMDocument`. See http://php.net/dom
Artefacto
A: 

After checking your code with two inputs:

$text1 = '<a href="http://www.yahoo.com"&gt;Yahoo&lt;/a&gt;';
$text2 = 'text text http://www.yahoo.com text';

$reg = '{
         (?<![">])
         (
           (?:http|ftp)s? : // [^<>\s]+
         )
        }ix';

echo  preg_replace($reg, '<A HREF="\1">\1</A>', $text1 ) . "\n";
echo  preg_replace($reg, '<A HREF="\1">\1</A>', $text2 ) . "\n";

and reading the corresponding outputs:

=> #1 not touched  <a href="http://www.yahoo.com"&gt;Yahoo&lt;/a&gt;
=> #2 modified     text text <A HREF="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/A&gt; text

I'd ask you what you think what should be different?

Regards

rbo

rubber boots
#1 should output like <a href=" <a href="http://www.yahoo.com/"">http://www.yahoo.com/"</a>>Yahoo</a>
jason
There was space after <a href="<SPACE> so the rule was ignoring it. I added \s and its working now
jason