Php 匹配由3个或更多标记组成的组中的图像标记

Php 匹配由3个或更多标记组成的组中的图像标记,php,regex,preg-match,preg-match-all,Php,Regex,Preg Match,Preg Match All,我正试图得到的图像标签,出现在一堆至少3如下 $str = " <img>Some image</img> Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text <img>Some image</img> Text Text Text Text Text Text Text

我正试图得到的图像标签,出现在一堆至少3如下

$str = "

<img>Some image</img>

Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text 
<img>Some image</img>


Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text 

<img>Image i need</img>
<img>Image i need</img>
<img>Image i need</img>
<img>Image i need</img>

Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text

";
我尝试了以下方法

但是,这只是返回所有的图像标签,我试图匹配一些文本与多次出现的图像,但仍然可以得到它的工作…任何帮助是感激的家伙

感谢大家的回答,特别是zx81和avinash,但我也想知道这种类型的图像标签的相同正则表达式


再次感谢各位

你们需要用括号和空格捕捉比赛的某些部分。下面是一个带有一个捕获组的示例正则表达式:

preg_match_all("/<img/is", $str, $matches);

如果现在查看$matches,您会看到标记之间的文本存储在数组的一个条目中。

这可能就是您需要的

<img src='data' />
<img src='data' />
<img src='data' />
<img src='data' />

这将捕获所有img标记,并且仅当它们在一行中至少有3个时。这是有效的,经过测试。哦,还用了全球旗帜我想这就是你想要的

preg_match_all("/<img>(.+?)</img>/", $str, $matches);
它匹配三个或更多连续的图像标记

你的PHP代码是

(<img>.+?<\/img>\s*){3,}
输出:

/(?:<img\b[^\n]*\n){3,}/gm
如果要将它们放在单独的组索引中,请尝试以下方法

$regex =  '~(?:<img\b[^\n]*\n){3,}~';
preg_match_all($regex, $str, $matches);
echo var_dump($matches);
标签数组:一点定义工作 在正则表达式中逐个匹配图像标签有点困难,但可以做到:

<img>Image i need</img>
<img>Image i need</img>
<img>Image i need</img>
<img>Image i need</img>
在中,可以看到正确的标记一个接一个地匹配

打印所有匹配项的示例PHP代码:

扩展规范

对于标记特别以开头的相同模式,请使用以下代码:

(?<!<\/img>\n)<img\b[^\n]*\n<img\b[^\n]*\n(?!<img)|(?<!<\/img>\n)<img\b[^\n]*\n(?!<img)(*SKIP)(*F)|(<img>Image i need<\/img>)

您可能只需要更改正则表达式以匹配所有3个,如/+?\s*{3,99}/is。他不想一个接一个地匹配标记吗?是的,他想要连续的三个或更多img标记。我以为他想一次匹配一个,而不是在一个块中,这样他就可以拥有一个标记数组。嘿,这对带有php preg_match的/si mods很有用。我想知道你能不能让它返回3个匹配的img标记的不同数组索引中的匹配数据,如0、1、2……是的,我只想要那些出现在3+中的数据,但如果它能在数组的新索引中获得每个匹配数据,那就太好了:仅供参考,regex评论道,添加了演示,我没见过像这样的正则表达式代码,这是一些preg匹配:D非常感谢:Dhey如果图像标记是这样的,你能修改那里的代码吗?…我真的很感谢你的帮助事实上这是为我工作的正则表达式…稍微修改$regex=~?smx?DEFINE?]*'[^?:?=?&image?:\s*^?&image{2}|?:?很高兴看到您对它感到满意,这是一个很酷的正则表达式。谢谢您的绿色复选标记!
(?smx)   # free-spacing, DOTALL, multi-line

# Let's define an image tag
(?(DEFINE)(?<image><img[^>]*>[^<]*</img>))

(?:
  # If what follows is 3 images
  (?=(?&image)(?:\s*^(?&image)){2})
  # OR
  |
  # We're at a continuation point, but not the beginning of the string
  (?:(?<!\A)\G)      
  # Match and drop whitespaces
  \s*\K
)
# THEN Match an image!
(?&image)
$regex = '~(?smx)(?(DEFINE)(?<image><img[^>]*>[^<]*</img>))
(?:
  (?=(?&image)(?:\s*^(?&image)){2})
  |
  (?:(?<!\A)\G)\s*\K
)
(?&image)~';

if (preg_match_all($regex, $yourstring, $matches)) {
   print_r($matches[0]);
}
$regex = '~(?smx)(?(DEFINE)(?<image><img[ ]src='data'[ ]/>[^<]*</img>))
(?:
  (?=(?&image)(?:\s*^(?&image)){2})
  |
  (?:(?<!\A)\G)\s*\K
)
(?&image)~';

if (preg_match_all($regex, $yourstring, $matches)) {
   print_r($matches[0]);
}