在 PHP 中提取并转储 DOM 节点(及其子节点)
“我有以下场景,我已经花了几个小时尝试处理它:我正在开发一个 Wordpress 主题(因此是 PHP),我想检查帖子的内容(即 HTML)是否包含带有特定的 ID/类别。如果是这样,我想从内容中提取它并将其放在其他地方。 示例:假设 WordPress 帖子的文本内容是
<?php
/* $content actually comes from WP function get_the_content() */
$content = '<p>some text and so forth that I don\'t care about...</p> <div class="the-wanted-element"><p>I WANT THIS DIV!!!</p></div>';
?>
那么我如何使用类提取该 div(也可以给它一个 ID),将其(带有标签和所有内容)输出到模板的一个位置,以及将其余部分(当然不包括提取的标签)输出到模板的另一个位置? 我已经尝试过 DOMDocument 类,对我来说是皮塔,也许我太愚蠢了。
’I have the following scenario and I'm already spending hours trying to handle it: I'm developing a Wordpress theme (hence PHP) and I want to check whether the content of a post (which is HTML) contains a tag with a certain id/class. If so, I want to extract it from the content and place it somewhere else.
Example: Let's say the text content of the Wordpress post is
<?php
/* $content actually comes from WP function get_the_content() */
$content = '<p>some text and so forth that I don\'t care about...</p> <div class="the-wanted-element"><p>I WANT THIS DIV!!!</p></div>';
?>
So how can I extract that div with the class (could also live with giving it an ID), output it (with tags and all that) in one place of the template, and output the rest (without the extracted tag, of course) in another place of the template?
I've already tried with the DOMDocument class, p.i.t.a. to me, maybe I'm too stupid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
如何获取剩余的 xml/html:
Try:
How to get the remaining xml/html: