2016-04-26 13 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(); 

Ich möchte die URL aus src Attribut extrahieren. Und ich bekomme folgende in .preg_match - Warum zwei identische Elemente in Übereinstimmungen

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 
) 

Ich habe, was ich brauchte, aber warum gibt es zwei identische Artikel bei [1] und [2]? Wie kann ich das vermeiden?

+1

'((. *?))' 2x einfangenden Gruppen == 2x Ergebniselemente – Rizier123

+0

Sie sollten verwenden '$ str' '$ Karte'. –

+0

@ Rizier123, Sie haben Recht –

Antwort

0

Entfernen Sie einfach die $map, verwenden Sie $str unter preg_match('/src\=\"((.*?))\"/i',$map, $matches);. Beenden Sie die Verwendung von double capturing group Ihres Ergebnisses.

Versuchen Sie, diese

$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); 

Ergebnis

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

Warum sollte OP * dieses * verwenden? Was hast du verändert? Warum hast du es geändert? – Rizier123

+0

@ Rizier123, füge ich eine Beschreibung über das Ergebnis hinzu. –

1

Entfernen Sie den zusätzlichen Klammersatz um .*?. Sie definieren eine Erfassungsgruppe, und jetzt haben Sie eine Erfassungsgruppe in einer Erfassungsgruppe, daher zwei gleiche Ergebnisse.

Verwandte Themen