Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match-为什么比赛中有两个相同的项目_Php_Preg Match - Fatal编程技术网

Php preg_match-为什么比赛中有两个相同的项目

Php preg_match-为什么比赛中有两个相同的项目,php,preg-match,Php,Preg Match,我得到了我所需要的,但为什么[1]和[2]中有两个相同的项目?如何避免这种情况?删除*?周围的额外括号集。它们定义了一个捕获组,现在您在捕获组中有了一个捕获组,因此得到了两个相同的结果。只需删除$map,使用$str在preg\u match('/src\=\'(*?)\“/i',$map,$matches)。停止使用结果的双捕获组 试试这个 $str = '<iframe src="https://www.google.com/maps/place/the-big-junky-map-u

我得到了我所需要的,但为什么[1]和[2]中有两个相同的项目?如何避免这种情况?

删除
*?
周围的额外括号集。它们定义了一个捕获组,现在您在捕获组中有了一个捕获组,因此得到了两个相同的结果。

只需删除
$map
,使用
$str
preg\u match('/src\=\'(*?)\“/i',$map,$matches)。停止使用结果的
双捕获组

试试这个

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

(.*)
2x捕获组==2x结果元素您应该在
$map
@Rizier123使用
$str
,您是对的,为什么OP应该使用这个?你改变了什么?你为什么要更改它?@Rizier123,我添加了一些关于结果的描述。
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
)
$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
)