HTML DOM跨度搜索

时间:2012-07-03 15:16:41

标签: php dom

我有一个像以下一样的HTML代码块

<div class="searchDetailPanelRgt">
  <p>
   <span>ID: 10605814  |  Jun 26, '12</span> 
   <br />                                       
   <a href="somelink" target="_blank">View on Map</a>
   <a target='_blank' href='somelink' ></a>
  </p>
</div>

我想在php中使用HTML DOM库来访问Jun 26,12值。

$html->find(???)

1 个答案:

答案 0 :(得分:0)

<?php

$html = <<<HTML
<div class="searchDetailPanelRgt">
  <p>
   <span>ID: 10605814  |  Jun 26, '12</span>
   <br />
   <a href="somelink" target="_blank">View on Map</a>
   <a target='_blank' href='somelink' ></a>
  </p>
</div>
HTML;

$document = new DOMDocument();
$document->loadHTML($html);

$spans = $document->getElementsByTagName("span");
$the_first_span = $spans->item(0);
$the_first_spans_content = explode(" | ", $the_first_span->textContent);

echo $the_first_spans_content[1];