繁体   English   中英

php简单的html dom在td中获取href id

[英]php simple html dom get a href id inside td

如何使用PHP简单html dom在href内获取“名称”或“ id”属性。 我还需要在“ h4”标签内添加“标题文字”。 请你帮助我好吗? 这是html:

<td>
<a href="../Vehicle?itemID=22995061&RowNumber=9&loadRecent=True" name="22995061" id="22995061">
<h4>title text</h4>
</a>
<p>
Stock#:
<text>example</text>
</p>
<p>BLA BLA</p>
<p> fffff  </p>
</td>

我尝试过类似的方法,但它使我空白。

IDs = array();  
    $url = "http://someurl";
    $html = file_get_html(url);
foreach($html->find('h4') as $e)
 {

     echo $e->innertext."<br>";
     $dataID = $e->innertext; 
     $IDs[] = $dataID; 

 }

首先,改变

IDs = array();  

至,

$IDs = array();  

然后,为什么不使用DOMDocument类而不是正则表达式。 只需加载您的DOM,然后使用getElementsByTagName即可获取标签。 这样,您可以排除不需要的任何其他标签,而仅获得您要做的那些标签。

<?php
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<books>
 <book>Patterns of Enterprise Application Architecture</book>
 <book>Design Patterns: Elements of Reusable Software Design</book>
 <book>Clean Code</book>
</books>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
    echo $book->nodeValue, PHP_EOL;
}
?>

阅读材料

DOMDocument

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM