【发布时间】:2015-03-27 09:55:21
【问题描述】:
我在 PHP 中使用以下正则表达式从字符串中获取 URL
regex = '/https?\:\/\/[^\" ]+/i';
$string = "lorem ipsum http://google.com lorem ipusm dolor http://yahoo.com/something";
preg_match_all($regex, $string, $matches);
$urls = ($matches[0]);
$urls 返回所有 URL。怎么能只返回第一个 URL?在这种情况下http://google.com。
我试图在不使用 foreach 循环的情况下实现这一目标。
【问题讨论】:
-
@LalitSharma 不是重复的
-
@CyberJunkie 改进版将是
'~https?://[^" ]+~i'