2016-04-26 41 views
-1
$str = '<iframe src="https://www.google.com/maps/place/the-big-junky-map-url-with-lat-lon-etc-etc" width="100%" height="350" frameborder="0" style="border:0;" allowfullscreen></iframe>'; 
$matches = array(); 
preg_match('/src\=\"((.*?))\"/i',$map, $matches); 
echo '<pre>';print_r($matches);die(); 

我想从src属性中提取URL。我在$matchespreg_match - 为什么两个相同的项目匹配

Array 
(
    [0] => src="https://www.google.com/maps/place/the-big-junky-map-url-with-lat-lon-etc-etc" 
    [1] => https://www.google.com/maps/place/the-big-junky-map-url-with-lat-lon-etc-etc 
    [2] => https://www.google.com/maps/place/the-big-junky-map-url-with-lat-lon-etc-etc 
) 

我得到了我所需要的,但为什么有两个相同的[1] [2]?我怎样才能避免这种情况?

+1

'((。*?))'2个捕获组== 2个结果的要素 – Rizier123

+0

你应该使用'$ str' '$ map'。 –

+0

@ Rizier123,你对了 –

回答

0

只要删除$map,使用$strpreg_match('/src\=\"((.*?))\"/i',$map, $matches);。停止使用你的结果double capturing group

试试这个

$str = '<iframe src="https://www.google.com/maps/place/the-big-junky-map-url-with-lat-lon-etc-etc" width="100%" height="350" frameborder="0" style="border:0;" allowfullscreen></iframe>'; 
$matches = array(); 
preg_match('/src\=\"(.*?)\"/i',$str, $matches); 

echo '<pre>'; 
print_r($matches); 

结果

Array 
(
    [0] => src="https://www.google.com/maps/place/the-big-junky-map-url-with-lat-lon-etc-etc" 
    [1] => https://www.google.com/maps/place/the-big-junky-map-url-with-lat-lon-etc-etc 
) 
+0

OP *为什么要用这个*?你改变了什么?你为什么改变它? – Rizier123

+0

@ Rizier123,我添加了一些关于结果的描述。 –

1

删除.*?附近的多余括号。他们定义了一个捕获组,现在您在捕获组中拥有一个捕获组,因此有两个相同的结果。

相关问题