且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用PHP从字符串中找到URL

更新时间:2023-02-23 08:54:01

一种与http/https/ftp/ftps/scp/scps链接的有效方法: $newStr = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/]+!', "<a href=\"\\0\">\\0</a>",$str);

A working method for linking with http/https/ftp/ftps/scp/scps: $newStr = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/]+!', "<a href=\"\\0\">\\0</a>",$str);

我强烈建议在只有点的情况下不要链接,因为它将考虑PHP 5.2,ASP.NET等链接,这是很难接受的.

I strongly advise NOT linking when it only has a dot, because it will consider PHP 5.2, ASP.NET, etc. links, which is hardly acceptable.

更新:如果需要www.字符串,请查看.

Update: if you want www. strings as well, take a look at this.