Let me introduce you to a piece of PHP code that is used to determine the URL address and automatically convert it into a hyperlink. It uses a regular expression to match the URL in a string, and then converts the URL into a hyperlink and clicks to access the address. Friends in need can refer to it.
The code is as follows: <?php /** * php正则判断url 自动转换为超链接 * site: bbs.it-home.org */ function autolink($foo) { $foo = eregi_replace('(((f|ht)黑蕉tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '/1', $foo); if( strpos($foo, "http") === FALSE ){ $foo = eregi_replace('(www.[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '/1', $foo); }else{ $foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '/1/2', $foo); } return $foo; } ?> Copy after login |