Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP文档解析HTML?_Php_Html_Parsing_Domdocument - Fatal编程技术网

如何使用PHP文档解析HTML?

如何使用PHP文档解析HTML?,php,html,parsing,domdocument,Php,Html,Parsing,Domdocument,我这里有一个HTML块: <div class="title"> <a href="http://test.com/asus_rt-n53/p195257/"> Asus RT-N53 </a> </div> <table> <tbody> <tr> <td class="price-status">

我这里有一个HTML块:

<div class="title">
    <a href="http://test.com/asus_rt-n53/p195257/">
        Asus RT-N53
    </a>
</div>
<table>
    <tbody>
        <tr>
            <td class="price-status">
                <div class="status">
                    <span class="available">Yes</span>
                </div>
                <div name="price" class="price">
                    <div class="uah">758<span> ua.</span></div>
                    <div class="usd">$&nbsp;62</div>
                </div>

一个丑陋的解决方案是将价格结果转换为只保留数字:

echo (int) $price->nodeValue;
或者,您可以查询以查找div内的跨度,并将其从price(prices foreach内)中删除:

编辑:

要检索链接,只需使用
getAttribute()
并获取
href
链接:

$model->getAttribute('href')

你当前的代码有什么问题?所以我得到了一个名字和价格,但我需要更多和一个链接。而这样的单位在一个页面上并不多,因为它可以在一个循环中做什么呢?现在看起来像是-名字-价格--价格,必要时:-名称-价格-链接您必须阅读
href
属性。也许这有帮助:。所有东西的价格都可以,但如何获得链接?">
$span = $xpath->query('//div[@class="uah"]/span')->item(0);
$price->removeChild($span);
echo $price->nodeValue;
$model->getAttribute('href')