$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();
I want to extract the URL from src
attribute. And I get following in $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
[2] => https://www.google.com/maps/place/the-big-junky-map-url-with-lat-lon-etc-etc
)
I got what I needed, but why are there two identical items at [1] and [2]? How can I avoid this?
Just remove the $map
, use $str
at preg_match('/src\=\"((.*?))\"/i',$map, $matches);
. Stop using double capturing group
of your result.
Try this
$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);
Result
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
)