0

我正在使用 PHP Simple HTML DOM Parser我有一些 HTML 代码,我想从中提取第一个数字出现,在这个例子中是' 735438':

<!-- Some Tds without onclick atribute ->
<td onclick="StatsAnnonce ('735438');" style="CURSOR:pointer;"  onmouseover="return escape('<img  src=/images/icon_stats.gif border=0 width=13 height=14> <b>Statistiques de cette annonce</b><li>Affichages des détails annonce</li><li>Classement par mois et par pays.</li>');">&nbsp;&nbsp;<img  src="/images/icon_stats.gif" border="0" width="13" height="14" alt="Statistique annonce">&nbsp;Stats&nbsp;</td>
<td onclick="ModifAnnonce ('735438','1','302528');" style="CURSOR:pointer;"  onmouseover="return escape('<img  src=/images/button_edit.gif border=0 width=13 height=14> <b>Editer cette annonce</b><li>Modifier : titre, texte, prix</li><li>Ajouter/Supprimer des photos</li>');">&nbsp;&nbsp;<img  src="/images/button_edit.gif" border="0" width="13" height="14" alt="Modifier l'annonce">&nbsp;Modif&nbsp;</td>
<td onclick="ProlongerAnnonce ('735438');" style="CURSOR:pointer;"  onmouseover="return escape('<img  src=/images/button_calendar.gif border=0 width=14 height=14> <b>Renouveler cette annonce</b>');"><img  src="/images/button_calendar.gif" border="0" width="14" height="14" alt="Renouveler l'annonce">&nbsp;Renouv&nbsp;</td>
<td onclick="DeleteAnnonce('735438','06/08/2012 00:50','302528','Bon appart s+1');" style="CURSOR:pointer;"  onmouseover="return escape('<img  src=/images/button_trash.gif border=0 width=13 height=14> <b>Supprimer cette annonce</b>');"><img  src="/images/button_trash.gif" border="0" width="13" height="14" alt="Supprimer l'annonce">&nbsp;Suppr&nbsp;</td>

我已尝试使用此代码(根据文档说明)但无法正常工作:

$html = str_get_html('page.html');

// Find all <td> with the 'onclick' as attribute
$ret = $html->find('td[onclick]');


// Find just all <td>
$ret = $html->find('td');


foreach($ret as $val)
{
echo $val."<br/>";
}

结果只是一个没有代码的空白页面......我也是新的 HTML DOM Parser,如果有人使用过这个库,请帮助我......

提前致谢

4

1 回答 1

1

First, I'd make sure that you're actually reading in the document (I'm pretty sure you're not at this point). You should be using:

$html = file_get_html('page.html');  // This!
//$html = str_get_html('page.html'); // Not this!

Assuming you're trying to extract information from the onclick attribute, it seems you should use this in your foreach loop:

echo $val->onclick . "<br/>";  // This!
//echo $val."<br/>";  //Not this!

$val is still an html element at this point - I'm not sure what you'll get if you echo it out, but I'm guessing it's not what you're looking for.

于 2012-08-24T01:25:44.193 回答