首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP简单html dom,只选择内容的一部分。

PHP简单html dom,只选择内容的一部分。
EN

Stack Overflow用户
提问于 2020-08-26 10:27:57
回答 1查看 71关注 0票数 0

我正在使用简单的html dom来解析html。https://simplehtmldom.sourceforge.io/

给定以下代码

代码语言:javascript
复制
<div class="item-price offer-price price-tc default-price">
$129.990
<span class="discount-2">-35%</span>
</div>

怎样才能只选择价格?我使用的是$html->find(div.offer-price, 0)->plaintext;,但它也选择了跨度的内容。

EN

回答 1

Stack Overflow用户

发布于 2020-08-27 21:33:47

不知道如何在简单的how中做,但是你可以使用DOMDocument + DOMXPath来解压它。

代码语言:javascript
复制
<?php

$html='<div class="item-price offer-price price-tc default-price">
$129.990
<span class="discount-2">-35%</span>
</div>
';
echo  (new DOMXPath(@DOMDocument::loadHTML($html)))->query("//div[contains(@class,'item-price')]/text()")->item(0)->textContent;

额外的好处: DOMDocument和DOMXPath都是php内置的,使用em不需要外部库。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63589618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档