How can we get specific links using simple html dom
-
14-11-2019 - |
Question
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.
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow