根据父级元素将 HTML 字符串拆分为数组

问题描述:

我需要转换 the_content() 返回的 HTML 字符串;在 Wordpress 中到每个父级元素的数组.例如:

I need to convert the HTML string returned by the_content(); in Wordpress to an array of each parent-level element. For example:

<h3>My subtitle</h3>
<p>Some content here</p>
<blockquote><p>A blockquote goes here</p></blockquote>

会变成:

array['<h3>My subtitle</h3>', '<p>Some content here</p>', '<blockquote> <p>A blockquote goes here</p></blockquote>']
<p>一个块引用在这里</p></blockquote>']

The reason we want to do this is to insert an ad into the content-- after the first paragraph if the first paragraph or content block is greater than 670 characters, or after the second paragraph if the content is shorter than that. The challenge is if either of those paragraphs are wrapped by another element, or if another element is involved at all.

我们要这样做的原因是在内容中插入广告——如果第一段或内容块大于 670 个字符,则在第一段之后,如果内容短于第二段,则在第二段之后.挑战在于这些段落中的任何一个是否被另一个元素包裹,或者是否涉及另一个元素.

This is the code I currently have:

这是我目前拥有的代码:

$content = apply_filters('the_content', get_the_content()); $content = explode("</p>", $content); $firstParagraphLength = strlen($content[0]); if($firstParagraphLength > 670) { $paragraphAdAfter = 1; } else { $paragraphAdAfter = 2; } // If this is after the target paragraph, insert ad code first for ($i = 0; $i <count($content); $i++) { if ($i == $paragraphAdAfter) { ?> <!-- AD CODE --> My ad code goes here, great! <?php } echo $content[$i] . "</p>"; } ?>

This actually works, but if a blockquote is involved in either the first paragraph or the second, the ad is inserted into the blockquote element.  The data is pretty dynamic, so I need to figure out a way to split based on the parent-level elements, whether they are blockquotes, paragraphs, headlines, etc.

这确实有效,但如果第一段或第二段中涉及块引用,则广告会插入到块引用元素中.数据是非常动态的,所以我需要想办法根据父级元素进行拆分,无论它们是块引用、段落、标题等.

解决方案