我有一个包含URL和其他文本的字符串。我想将所有的URL都存入$matches
数组中。但是,下面的代码将无法获得全部的URL中$matches
阵列:从字符串中获取所有网址?
$matches = array();
$text = "words cotry.lk and newe.com joemiller.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[-A-Z0-9+&@#/%?=~_|!:,.;][.]*[-A-Z0-9+&@#/%=~_|(https?|ftp|file)://-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%?=~_|!:,.;]{8,50}$i', $text, $matches);
print_r($matches);
上面的代码不会告诉我以下网址:
cotry.lk
newe.com
你能告诉我一个例子,如何我可以修改上面的代码来获取所有的URL。
请注意,并非所有的URL都包含herf,并且这个字符串不是从html文件中获取的。
对于您的情况,您的正则表达式只匹配网址,因为它们的长度 - 它也匹配长度超过8个字符的任何其他单词 – 2013-04-27 12:29:50