簡體   English   中英

當我只有div的名稱時,php簡單的html dom href

[英]php simple html dom href when i only have name of div inside it

大家好,我想嘗試進入下面代碼中的鏈接,但是唯一帶有名稱的div是在href內。 我知道如何到達href,如果它在div內,但不是這樣。 有人可以幫忙嗎?

<div>
<a href="/economic-research/blog/EconomicPublications/algeria-country-report-mar17.pdf">
                <div class="CountryRiskReportLink">
                        Algeria Country Report
                </div></a>
</div>

在simple-html-dom中,您可以使用parent()

$div = $doc->find('.CountryRiskReportLink', 0);
echo $div->parent()->href;

是的,您可以在沒有JQuery的情況下獲取href屬性的值。 這是簡單的代碼段。 嘗試這個,

$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();

希望這個能對您有所幫助!

我怎么找到最后一個<div class>在帶有 PHP 簡單 HTML DOM 解析器的 HTML 文件中?</div><div id="text_translate"><p> 根據<a href="http://simplehtmldom.sourceforge.net/" rel="nofollow noreferrer">SIMPLE HTML DOM PARSER</a>的文檔(在“How to modify HTML Elements”選項卡下),這段代碼找到了<div class="hello">的第一個實例:</p><pre> $html = str_get_html('<div class="hello">Hello</div><div class="world">World</div>'); $html->find('div[class=hello]', 0)->innertext = 'foo'; echo $html; // Output: <div class="hello">foo</div><div class="world">World</div></pre><p> 如果我想在<div class="hello">的<em>最后一個</em>實例中插入 'foo' 怎么辦,假設 HTML 代碼有很多<div class="hello">實例。</p><p> 什么應該取代0 ?</p></div>

[英]How do I find the last <div class> in an HTML File with PHP Simple HTML DOM Parser?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM