Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 正则表达式导致重复,为什么?_Php_Regex - Fatal编程技术网

Php 正则表达式导致重复,为什么?

Php 正则表达式导致重复,为什么?,php,regex,Php,Regex,我试图捕获@font-face中的所有URL,但$matches会创建重复的URL,而它不应该这样做。我真的不明白为什么会这样。我用创建了正则表达式,它工作得很好,但当我自己将它放在php文档中时,它就不工作了。非常感谢您的帮助! 如果有帮助的话,我的php版本是7.1 <?php $re = '/url\s*\(\s*[\'"]?(?!(((?:https?:)?\/\/)|(?:data\:?:)))([^\'"\)]+)[\'"]?\s*\)/'; $str = '@font-fac

我试图捕获@font-face中的所有URL,但$matches会创建重复的URL,而它不应该这样做。我真的不明白为什么会这样。我用创建了正则表达式,它工作得很好,但当我自己将它放在php文档中时,它就不工作了。非常感谢您的帮助! 如果有帮助的话,我的php版本是7.1

<?php
$re = '/url\s*\(\s*[\'"]?(?!(((?:https?:)?\/\/)|(?:data\:?:)))([^\'"\)]+)[\'"]?\s*\)/';
$str = '@font-face{font-family:Poiret One;font-style:normal;font-weight:400;src:local(\'PoiretOne-Regular\'),url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf) format(\'truetype\')}@font-face{font-family:Open sans;font-style:normal;font-weight:400;src:local(\'OpenSans-Regular\'),url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/OpenSans-Regular.ttf) format(\'truetype\')}@font-face{font-family:Open sans;font-weight:300;src:local(\'OpenSans-Light\'),url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/OpenSans-Light.ttf) format(\'truetype\')}@font-face{font-family:Dancing Script;font-weight:400;font-style:normal;src:local(\'dancing-script_regular\'),url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/dancing-script_regular_webfont.ttf) format(\'truetype\')}@font-face{font-family:Josefin Slab;font-weight:400;font-style:normal;src:local(\'JosefinSlab-Regular\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Regular.ttf) format(\'truetype\')}@font-face{font-family:Josefin Slab;font-weight:400;font-style:italic;src:local(\'JosefinSlab-Italic\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Italic.ttf) format(\'truetype\')}';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
print_r($matches);
我刚刚运行了你的代码

我得到了一个由18个元素组成的数组

Ctrl+F返回两个“divi-child”实例的原因是,它出现在完整内容匹配中,以及用括号指定的较小匹配中

array (size=18)
  0 => 
    array (size=4)
      0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2)' (length=72)
      1 => string '' (length=0)
      2 => string '' (length=0)
      3 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2' (length=67)
  1 => 
    array (size=4)
      0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff)' (length=71)
      1 => string '' (length=0)
      2 => string '' (length=0)
      3 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff' (length=66)
<snip>
array (size=2)
  0 => 
    array (size=18)
      0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2)' (length=72)
      1 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff)' (length=71)
      2 => string 'url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf)' (length=62)
      3 => string 'url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2)' (length=79)
      <snip>
  1 => 
    array (size=18)
      0 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2' (length=67)
      1 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff' (length=66)
      2 => string '/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf' (length=57)
      3 => string '/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2' (length=74)
      <snip>
将发送到
preg\u match\u all()
函数的参数更改为

preg_match_all($re, $str, $matches);
返回的值将是一个包含两个元素的数组(每个数组本身)。第一个元素是和正则表达式完全匹配的数组。第二个元素是我们用括号捕获的匹配部分

array (size=18)
  0 => 
    array (size=4)
      0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2)' (length=72)
      1 => string '' (length=0)
      2 => string '' (length=0)
      3 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2' (length=67)
  1 => 
    array (size=4)
      0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff)' (length=71)
      1 => string '' (length=0)
      2 => string '' (length=0)
      3 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff' (length=66)
<snip>
array (size=2)
  0 => 
    array (size=18)
      0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2)' (length=72)
      1 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff)' (length=71)
      2 => string 'url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf)' (length=62)
      3 => string 'url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2)' (length=79)
      <snip>
  1 => 
    array (size=18)
      0 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2' (length=67)
      1 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff' (length=66)
      2 => string '/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf' (length=57)
      3 => string '/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2' (length=74)
      <snip>
数组(大小=2)
0 => 
阵列(大小=18)
0=>string'url(/wp content/themes/Divi Child/font/poiretone-regular_webfont.woff2)'(长度=72)
1=>string'url(/wp content/themes/Divi Child/font/poiretone-regular_webfont.woff)'(长度=71)
2=>string'url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf)'(长度=62)
3=>string'url(/wp-content/themes/Divi-Child/font/opensans-regular-webfont_-webfont.woff2)'(长度=79)
1 => 
阵列(大小=18)
0=>string'/wp content/themes/Divi Child/font/poiretone-regular_webfont.woff2'(长度=67)
1=>string'/wp content/themes/Divi Child/font/poiretone-regular_webfont.woff'(长度=66)
2=>string'/wp content/themes/Divi Child/font/PoiretOne Regular.ttf'(长度=57)
3=>string'/wp content/themes/Divi Child/font/opensans-regular-webfont_webfont.woff2'(长度=74)

你好,卢克,谢谢你帮助我!我们如何才能只捕获较小的匹配项,这样我们就只有一个实例?使用非贪婪的量词,如
*?
,而不是贪婪的量词,如
[^”]
+
等。请查看我的答案-我已经扩展了它,为您提供了一个可能的解决方案,@skillybastarddit在中解释并做了相同的操作:
$matches[0]
包含与整个
regex
相匹配的子字符串,从
$matches[1]
开始,有一些子字符串与
regex
中的当前值相匹配。如果查看
元素中的
print\r($matches)
的输出(或者根本没有嵌入HTML),您可以很容易地发现这一点谢谢@axiac我把这个和卢克的答案一起用了!