2013-10-31 54 views
2

我有这样的场景:获取的值id用PHP简单的HTML DOM

<div class="listing_title"> 
    <strong> 
     <a href="http://www.mywebsite.com/dectails23291.html" id="url_id_1977"> 
     Listing Title 
     </a> 
    </strong> 
</div> 

要获得上市标题,我已经实现了这个代码:

$page = "http://www.mywebsite.com/listings.html"; 
$html = new simple_html_dom(); 
$html->load_file($pagina); 

foreach($html->find('.listing_title') as $element) 
    echo $element->first_child()->plaintext . '<br>'; 

输出为:

Listing Title 

现在我需要得到id值

url_id_1977

最好只有“1977”,干净的“url_id_”,但我不知道这样做。提前致谢!!

+0

在foreach循环无法正常工作,我得到这个错误:试图让非对象 –

+0

我需要得到相同的foreach ID值高档印刷品的属性,与此输出: 清单标题 1977年 –

回答

1

添加您foreach循环这里面:

echo end(explode('_', $element->find('a', 0)->id));

为了摆脱的警告,你可以分配id给一个变量:

$id = explode('_', $element->find('a', 0)->id); 
echo $id[2]; 

或者,如果您主播的id总是以url_id_开头,只需使用str_replace()

echo str_replace('url_id_', '', $element->find('a', 0)->id); 
+0

选择器工作非常好!只有结束和爆炸的问题,因为我得到这个错误:严格的标准:只有变量应该通过引用传递。但是你的代码(关于选择器)非常棒! –

+0

@vincenzolopalo啊好抓。我更新了我的答案 – billyonecan

+0

非常好!谢谢! –

0
try this 

foreach($html->find('*[class=listing_title] a') as $element) 
    echo $element->id. '<br>'; 
+0

使用此代码我只在输出列表标题,而不是id值 –

+0

对不起,尝试像这样foreach($ html-> find('* [class = listing_title]')as $ element) echo $ element-> id。 '
'; – Raghavendra

+0

对不起,我错过了“a”试试这个foreach($ html-> find('* [class = listing_title] a')as $ element) echo $ element-> id。 '
'; – Raghavendra

相关问题