2016-04-26 71 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

刪除.*?附近的多餘括號。他們定義了一個捕獲組,現在您在捕獲組中擁有一個捕獲組,因此有兩個相同的結果。

相關問題