如果元素没有类,如何获取元素的内容?
我正在使用 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>
的内容而没有任何课程
来自Simple 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工作正常,但不是代码???
有任何线索吗? 感谢
答案 0 :(得分:3)
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
修改强>
在检查原始代码之后,这里有一种获取所需部件的方法......只是为了给你一个想法,当然你仍然可以改进它:
// 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);
OUPUT
1 => <pre><code><div class="uk-grid">...
5 => <pre><code><div class="uk-grid">...
8 => <pre><code><div class="uk-grid uk-g...
10 => <pre><code><div class="uk-grid">...
12 => <pre><code><div class="uk-grid" data-uk-grid-match>...</div></code></pre>
14 => <pre><code><div class="uk-grid" data-uk-grid-match=...
16 => <pre><code><ul class="uk-grid" data-uk-grid-margin> <!-- Th