最好的方法是使用正则表达式。php代码:
$text = "FTP Source: ftp://ftp.testftp.com/ -
My mail adress is name@yahoo.com and
I love www.facebook.com/
I search things using http://www.google.fr/";
$urlRegularExpression = "/([fhtps]*:\/\/)?([a-zA-Z0-9\.-]*\.[a-zA-Z]{2,6})/";
preg_match_all($urlRegularExpression, $text, $urls);
print_r($urls);
结果:
Array
(
[0] => Array //THE WHOLE RESULTS
(
[0] => 'ftp://ftp.testftp.com'
[1] => 'www.facebook.com'
[2] => 'http://www.google.fr'
)
[1] => Array //THE PROTOCOLS USED (ftp, http, sftp, https)
(
[0] => 'ftp://'
[1] => ''
[2] => 'http://'
)
[2] => Array //THE ADRESS WITHOUT PROTOCOL
(
[0] => 'ftp.testftp.com'
[1] => 'www.facebook.com'
[2] => 'www.google.fr'
)
)
编辑:您可以在正则表达式的 :// 之前移动 ) 以仅获取协议名称:
$urlRegularExpression = "/([fhtps]*):\/\/?([a-zA-Z0-9\.-]*\.[a-zA-Z]{2,6})/";
示例:http:// -> http