3

我有一个包含 URL 和其他文本的字符串。我想将所有 URL 放入$matches数组中。但是以下代码不会将所有 URL 放入$matches数组中:

$matches = array();
$text = "soundfly.us schoollife.edu hello.net some random news.yahoo.com text http://tinyurl.com/9uxdwc some http://google.com random text http://tinyurl.com/787988 and others will en.wikipedia.org/wiki/Country_music URL";
preg_match_all('$\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]$i', $text, $matches);
print_r($matches);

上面的代码将得到:

http://tinyurl.com/9uxdwc
http://google.com
http://tinyurl.com/787988

.

但错过了以下 4 个网址:

schoollife.edu 
hello.net 
news.yahoo.com
en.wikipedia.org/wiki/Country_music

你能告诉我一个例子,我怎样才能修改上面的代码来获取所有的 URL

4

1 回答 1

1

这是你需要的吗?

$matches = array();
$text = "soundfly.us schoollife.edu hello.net some random news.yahoo.com text http://tinyurl.com/9uxdwc some http://google.com random text http://tinyurl.com/787988 and others will en.wikipedia.org/wiki/Country_music URL";
preg_match_all('$\b((https?|ftp|file)://)?[-A-Z0-9+&@#/%?=~_|!:,.;]*\.[-A-Z0-9+&@#/%=~_|]+$i', $text, $matches);
print_r($matches);

我将协议部分设置为可选,添加了使用分隔域和 TLD 的点和“+”以获取该点之后的完整字符串(TLD + 额外信息)

结果是:

[0] => soundfly.us 
[1] => schoollife.edu 
[2] => hello.net 
[3] => news.yahoo.com 
[4] => http://tinyurl.com/9uxdwc 
[5] => http://google.com 
[6] => http://tinyurl.com/787988 
[7] => en.wikipedia.org/wiki/Country_music

也适用于 IP 地址,因为必须存在一个点。用字符串“192.168.0.1”和“192.168.0.1/test/index.php”测试

于 2014-06-10T17:00:00.747 回答