Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

convert URLS to links in PHP (See related posts)

// In PHP, detect plaintext URLs, all the way from example.com to https://username:password@sub.domain.com:9999/dir/ect/ory/file?param=val&parm2=val2 and wrap them with href tags.

$pattern = "@\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@";

$text_with_hyperlink = preg_replace($pattern, '<a href="\0">\0</a>', $text_with_raw_URLs);

Comments on this post

You need to create an account or log in to post comments to this site.


Related Posts