0

Hey guys so i am trying to get to the link in the code below however the only div with a name is inside the href. I know how to get to the href if it was inside the div but not this way around. Can anyone help?

<div>
<a href="/economic-research/blog/EconomicPublications/algeria-country-report-mar17.pdf">
                <div class="CountryRiskReportLink">
                        Algeria Country Report
                </div></a>
</div>
  • you want value inside href attribute? Please clear you requirement – Geee Oct 11 '17 at 16:13
  • sorry yes i want to be able to get /economic-research/blog/EconomicPublications/algeria-country-report-mar17.pdf when i cant only reference CountryReportLink – user3105661 Oct 11 '17 at 16:18
  • and sorry the unnamed Div that it is in is inside of a webpage with multiple other unnamed Divs which is why CountryReportLink is the only reference that i have – user3105661 Oct 11 '17 at 16:32

2 Answers2

1

In simple-html-dom you would use parent():

$div = $doc->find('.CountryRiskReportLink', 0);
echo $div->parent()->href;
pguardiario
  • 53,827
  • 19
  • 119
  • 159
0

Yes, You can get the value of href attribute without JQuery. Here is the simple snippet. Try this,

$a = new SimpleXMLElement("<a href='/economic-research/blog/EconomicPublications/algeria-country-report-mar17.pdf'>
        <div class='CountryRiskReportLink'>
                Algeria Country Report
        </div>
    </a>");
echo $a['href']; // will echo /economic-research/blog/EconomicPublications/algeria-country-report-mar17.pdf
die();

Hope this will help you!

Geee
  • 2,217
  • 15
  • 30
  • The question seems to be asking for a PHP solution, not a Javascript one. – Nigel Ren Oct 11 '17 at 16:25
  • yes sorry is there a way to do this in PHP i have been using Simple Html Dom Parser – user3105661 Oct 11 '17 at 16:28
  • Check updated answer, for more check the [link](https://stackoverflow.com/questions/6365701/php-extract-link-from-a-tag) and [this](https://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element) – Geee Oct 11 '17 at 16:36
  • thanks ill which check the links provided. Sorry i was not clear enough. I cannot just put that into the code the link is on a page with lots of other links in the same format where then only named Div is inside the href so i need a way to say give me the link if the div inside is called CountryRiskReportLink – user3105661 Oct 11 '17 at 16:45