簡體   English   中英

在PHP簡單HTML DOM解析器中查找沒有類且沒有ID的div

[英]Find div that has no class and no id in PHP Simple HTML DOM Parser

我正在使用PHP Simple HTML DOM解析器,它運行良好,但是在選擇沒有ID AND CLASS的div遇到了問題。

$html->find( 'div[!class]' )

將返回所有沒有類但可以有ID的div。

我嘗試了這個但沒有成功。

$html->find( 'div[!class][!id]' )

提前致謝。

嘗試這個:

$html->find( 'div[!class], div[!id]' )

如果您看解析器的代碼,我認為它沒有這種功能。

function find($selector, $idx=null, $lowercase=false)
{
    $selectors = $this->parse_selector($selector);
    if (($count=count($selectors))===0) return array();
    $found_keys = array();

    // find each selector-here it checks for each selector rather than combined one
    for ($c=0; $c<$count; ++$c)
    {

所以你可以做的是

$find = $html->find('div[!class]');

$selected = array();
foreach ($find as $element) {
    if (!isset($element->id)) {
        $selected[] = $element;
    }
}

$find = $selected;

因此,現在查找將具有所有沒有id和class的元素。

我怎么找到最后一個<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