简体   繁体   English

PHP preg_match html 中的多个标签

[英]PHP preg_match multiple tags in html

I need to section off my html by multiple tags.我需要用多个标签分割我的 html。 from the start of the <h1> tag up to but not including the following <h1> tag.<h1>标记的开头到但不包括以下<h1>标记。 So far the expression I have works but only retrieves the first section.到目前为止,我的表达方式有效,但只检索第一部分。 looking specifically for a preg_match solution.专门寻找preg_match解决方案。 Would ideally like the solution to be dynamic (not matter the contents between the h1 tags or how many sections there are).理想情况下希望解决方案是动态的(无论 h1 标签之间的内容或有多少部分)。 Let me know (kinda new to regex in general).让我知道(一般来说,对正则表达式来说有点新)。 I know this may be a tricky question as I am new to php in general.我知道这可能是一个棘手的问题,因为我通常是 php 的新手。

html html

<h1>heading1</h1>
<img>
<p>para1</p>
<p>para2</p>
<h1>heading2</h1>
<img>
<p>para1</p>
<p>para2</p>
<h1>heading3</h1>
<img>
<p>para1</p>
<p>para2</p>

desired output or something similar:所需的 output 或类似的东西:

array{
  [0]=> <h1>heading1</h1>
        <img>
        <p>para1</p>
        <p>para2</p>

  [1]=> <h1>heading2</h1>
        <img>
        <p>para1</p>
        <p>para2</p>

  [2]=> <h1>heading3</h1>
        <img>
        <p>para1</p>
        <p>para2</p>
}

my current expression is:我现在的表达是:

$regex = '#<\/p>\s*(<h2>.*?)<h2>#s';
$preg = preg_match_all($regex, $content, $matches);
print_r ($matches);

Very thankful for any help!非常感谢您的帮助!

preg_match_all() won't return overlapping matches. preg_match_all()不会返回重叠匹配。 Since your regexp ends with <h2> , which is the start of the next match, it won't return that next match.由于您的正则表达式以<h2>结尾,这是下一场比赛的开始,因此它不会返回下一场比赛。

Put the start of the next match into a lookahead so it's not included in the match.将下一场比赛的开始放入前瞻中,使其不包括在比赛中。

$regex = '#<\/p>\s*(<h1>.*?)(?=<h1>)#s';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM