PHP: URL detection (regexp) includes line breaks

Posted by marco92w on Stack Overflow See other posts from Stack Overflow or by marco92w
Published on 2010-05-16T15:14:58Z Indexed on 2010/05/16 15:20 UTC
Read the original article Hit count: 217

Filed under:
|
|

I want to have a function which gets a text as the input and gives back the text with URLs made to HTML links as the output.

My draft is as follows:

function autoLink($text) {
    return preg_replace('/https?:\/\/[\S]+/i', '<a href="\0">\0</a>', $text);
}

But this doesn't work properly.

For the input text which contains ...

http://www.google.de/

... I get the following output:

<a href="http://www.google.de/<br">http://www.google.de/<br</a> />

Why does it include the line breaks? How could I limit it to the real URL?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about regex

Related posts about php