How can we get specific links using simple html dom

邮差的信 提交于 2019-12-12 03:23:08

问题


I have used this script which i found in the official simple html dom site to find hyperlinks in a website

foreach($html->find('a') as $element) 
       echo $element->href . '<br>';

it returned all the links found in the website but i want only specific links in that website. is there a way of doing it in simple html dom. This is the html code for that specific links

<a class="z" href="http://www.bbc.co.uk/news/world-middle-east-16893609" target="_blank" rel="follow">middle east</a>

where this is the html tag which is different from other hyperlinks

<a class="z"

and also there is any way i can get the link text ("middle east") together with the link.


回答1:


I understand you'd like all a elements with the class z? You can do that like this:

foreach($html->find('a.z') as $element)

You can get an element's value (which for links will be the link text) with the plaintext property:

$element->plaintext

Please note that this can all be found in the manual.



来源:https://stackoverflow.com/questions/9518368/how-can-we-get-specific-links-using-simple-html-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!