我正在使用简单的html dom来解析html。https://simplehtmldom.sourceforge.io/
给定以下代码
<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;,但它也选择了跨度的内容。
发布于 2020-08-27 21:33:47
不知道如何在简单的how中做,但是你可以使用DOMDocument + DOMXPath来解压它。
<?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不需要外部库。
https://stackoverflow.com/questions/63589618
复制相似问题