PHP Simple Html Dom获取div的纯文本,但避开所有其他标签

标签 php simple-html-dom

我使用 PHP Simple Html Dom 来获取一些 html,现在我有一个 html dom 就像下面的代码,我需要获取纯文本内部 div,但要避免 p 标签及其内容(只返回 111111),谁能帮忙我?提前致谢!

<div>
    <p>00000000</p>
    111111
    <p>22222222</p>
</div>

最佳答案

这取决于你所说的“避免 p 标签”是什么意思。

如果你只想删除标签,那么只需运行strip_tags()它应该可以满足您的需求。

如果您真的只想返回“11111”(即去除标签及其内容),那么这不是一个可行的解决方案。为此,像这样的东西可能起作用:

$myDiv = $html->find('div'); // wherever your the div you're ending up with is
$children = $myDiv->children; // get an array of children
foreach ($children AS $child) {
    $child->outertext = ''; // This removes the element, but MAY NOT remove it from the original $myDiv
}
echo $myDiv->innertext;

关于PHP Simple Html Dom获取div的纯文本,但避开所有其他标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25662569/

相关文章:

PHP - 作为助手的特征

php - 如何在 Phalcon 中将每个表格转换为伏特?

php - 如何在循环中停止侧边栏导航中的事件类

PHP 解析器 - 复制下一行(?)

php - PHP 上的 Google Calendar API v3 域范围委托(delegate)服务帐户

php - 在没有 SMTP 身份验证的情况下使用 PHPMailer 发送电子邮件

php - 从页面抓取时如何按顺序获取数据

javascript - 通过简单的html dom删除页面的所有href

php - 简单的 html dom : how get a tag without certain attribute

php - fatal error : Allowed memory size of 33554432 bytes exhausted