简体   繁体   中英

Group specific HTML-tags into sections

I've a string with a markup like this:

<h3></h3>
<p><span><strong></strong></span></p>
<p></p>
<div></div>
<p></p>
<p></p>
<img />
<div></div>

And I want to group all tags into sections. But except divs and imgs. These both tags should be in their own section and should not be grouped together. So the result should be like this:

<section>
    <h3></h3>
    <p><span><strong></strong></span></p>
    <p></p>
</section>

<section>
    <div></div>
</section>

<section>
    <p></p>
    <p></p>
</section>

<section>
    <img />
</section>

<section>
    <div></div>
</section>

How can I do this with php?

Assuming that the content you give is only in the body, you can use this:

$data = <<<'LOD'
<h3></h3>
<p><span><strong></strong></span></p>
<p></p>
<div></div>
<p></p>
<p></p>
<img />
<div></div>
<p><p><img /></p></p>
<!-- <img /> -->
<div> <div> </div> </div>
LOD;

$pattern = <<<'LOD'
~
(?(DEFINE)
    (?<comment> <!-- .*? --> )
    (?<cdata> \Q<![CDATA[\E .*? ]]> )
    (?<script_style> <s(cript|tyle)\b .*? </s\g{-1}> )
    (?<skip_list>
        \g<comment> | \g<cdata> | \g<script_style> 
    )

    (?<tag>
        <code\b .*? </code> | \g<self_closing_tag> |
        <(\w++) [^>]*+>
        (?> [^<]++ | \g<skip_list> | \g<tag> )*+
        </\g{-1}> 
    )
    (?<self_closing_tag> <(?:img|[bh]r)\b [^>]*+> )
    (?<other_tag> (?!<(?:img|div)\b) \g<tag>  )

    (?<div> (?=<div\b) \g<tag>)
    (?<img> (?=<img\b) \g<self_closing_tag>)

    (?<other_tags> \g<other_tag> (?>[^<]*+ \g<other_tag>)*+ )
)

\g<skip_list> (*SKIP)(*FAIL)
|
\g<div> | \g<img> | \g<other_tags>  

~xsi
LOD;


$result = preg_replace($pattern, "\n<section>\n$0\n</section>", $data);

echo htmlspecialchars($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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