php - preg_replace not working when string contains a hashtag

Posted by Steven on Stack Overflow See other posts from Stack Overflow or by Steven
Published on 2010-04-14T19:27:42Z Indexed on 2010/04/14 19:33 UTC
Read the original article Hit count: 326

Filed under:
|
|

I found a function online for turning a url within a string into a clickable link. However, when the url contains a hashtag it doesn't work. eg. http://www.bbc.co.uk/radio1/photos/fearnecotton/5759/1#gallery5759

Here's the part of the function concerned:

$ret = preg_replace(
    "#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#",
    "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>",
    $ret
);

$ret = preg_replace(
    "#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#",
    "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>",
    $ret
);

Any ideas? thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about preg