0

我想在使用简单 HTML dom 解析器解析期间打印内部文本

HTML 源代码:

<div id="ctl00_ContentPlaceHolder_Middle_RatingSummary1_Rating1_RatingPanel">
<img id="ctl00_ContentPlaceHolder_Middle_RatingSummary1_Rating1_RatingImage" title="(2.5 / 5)  : Above Average" src="../../../../../images/net/common/stars/transparent/2.5.png" alt="(2.5 / 5)  : Above Average" style="border-width:0px;" />
<span id="ctl00_ContentPlaceHolder_Middle_RatingSummary1_Rating1_RatingText" class="text med strong">(2.5 / 5)  : Above Average</span>
<a id="ctl00_ContentPlaceHolder_Middle_RatingSummary1_Rating1_RatingHelp" class="help"></a>

我想要这样的输出:

(2.5 / 5)  : Above Average

我试过了,但没有得到它:

php代码:

$ratings = $html->find('div[id=ctl00_ContentPlaceHolder_Middle_RatingSummary1_Rating1_RatingPanel] span')->outertext;

echo "$ratings[0]"; 
4

1 回答 1

0

查看:

$dom = new DOMDocument();
$html = $file_get_contents('myfile.html');
@$dom->loadHTML($html);
$a = dom->getElementsByTagName('img');
foreach($a as $tags)
{
$attr = $tags->getAttribute('title');
echo $attr
}

或者可以根据您的 html 输入直接尝试:

$attr = dom->getElementsByTagName('img')->getAttribute('title');
echo $attr
于 2013-08-24T09:47:52.320 回答