繁体   English   中英

PHP简单HTML DOM-从没有CSS类的元素获取文本

[英]PHP Simple HTML DOM - get text from element without css class

仅当元素没有类时,如何获取元素的内容?
我正在使用PHP Simple HTML DOM从外部页面获取内容。

$html = file_get_html('someurl/page.html');

foreach($html->find('code') as $element) {
    echo $element->plaintext . '<br>';
}

我从所有<code>标记中获取内容。 而且我不希望来自<code class="smth">内容,我只希望来自<code>内容,而没有任何类。
从简单DOM手册中:

// Find all element which class=foo
$ret = $html->find('.foo');

// Remove a attribute, set it's value as null! 
$e->href = null;

// Determine whether a attribute exist? 
if(isset($e->href)) 
        echo 'href exist!';

我试过了

if(isset($e->class)) {
        echo $element->plaintext. '<br>';
    } 

但这只是在输出中搜索类(可能是?),而不在外部页面中搜索。 所以它什么也没有回声

/////编辑

$html->find('.className')

仅当它不是<code>标记时,此行返回元素? Div和p可以正常工作,但不能执行代码???

有什么线索吗? 谢谢

I get content from all <code> tags. And I don't want content from <code class="smth">, I want content only from <code> without any class.

您可以使用[!attribute]匹配不具有指定属性的元素...在这种情况下,您应该尝试code[!class]

例如,下面的工作代码获取所有不具有target属性的锚点:

// includes Simple HTML DOM Parser
include "simple_html_dom.php";

$text = '<div>
            <a href="#" >OK 1</a>
            <a href="#" target="_blank">Not needed</a>
            <a href="#" >OK 2</a>
            <a href="#" target="_blank">Not needed</a>
            <a href="#" >OK 3</a>
            <a href="#" target="_blank">Not needed</a>
            <a href="#" >OK 4</a>
        </div>';

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($text);

// Get all anchors not having target as attribute
$anchors = $html->find('div a[!target]');

// loop and print nodes content
foreach( $anchors as $i => $anchor ) {

    echo "$i => ".$anchor->outertext."<br/>";
}

// Clear dom object
$html->clear(); 
unset($html);

输出:

0 => OK 1
1 => OK 2
2 => OK 3
3 => OK 4

Working DEMO

编辑:

在检查原始代码之后,这是获取所需零件的一种方法...只是为了给您一个想法,您当然仍然可以对其进行改进:

// includes Simple HTML DOM Parser
include "simple_html_dom.php";

$url = 'http://getuikit.com/docs/grid.html';

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load_file($url);

// Get all nodes with "tm-article-subtitle"...
$nodes = $html->find('.tm-article-subtitle');

// loop and print nodes content
foreach( $nodes as $i => $node ) {

    // Filter only those containing "Markup"
    if (stripos($node->plaintext, "Markup") !== false) {
        echo "<pre>$i => ";

        // The wanted code in pre can be 1 or 2 position far from "Markup"
        if(stripos($node->next_sibling()->tag, "pre") !== false)
            echo htmlentities($node->next_sibling()->outertext);

        elseif(stripos($node->next_sibling()->next_sibling()->tag, "pre") !== false)
            echo htmlentities($node->next_sibling()->next_sibling()->outertext);

        echo "</pre>";
    }

}

// Clear dom object
$html->clear(); 
unset($html);

输出

1 => <pre><code>&lt;div class="uk-grid"&gt;...
5 => <pre><code>&lt;div class="uk-grid"&gt;...
8 => <pre><code>&lt;div class="uk-grid uk-g...
10 => <pre><code>&lt;div class="uk-grid"&gt...
12 => <pre><code>&lt;div class="uk-grid" data-uk-grid-match&gt;...&lt;/div&gt;</code></pre>
14 => <pre><code>&lt;div class="uk-grid" data-uk-grid-match=...
16 => <pre><code>&lt;ul class="uk-grid" data-uk-grid-margin&gt;     &lt;!-- Th

Working DEMO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM