嗨,我正在从外部url加载内容。像这样。
$html=get_data($external_url);
其中get_data()是一个使用curl获取内容的函数。
现在,我想从不同的HTML元素,如h1,div,p,span通过使用他们的类或id的内部HTML。
例如如果来自外部url($html)的内容是这样的
<html>
<title></title>
<body>
<h1 class="title">I am title</h1>
<div id="content">
i am the content.
</div>
</body>
现在我想用class="title"获得HTML标签的内部HTML。同样,我想获得标签的内部HTML id="content"
如何使用php做到这一点?我不了解DOM, XML。请帮助。
这是一个函数DOMDocument::saveHTML()
。在当前的php版本中,可以使用希望保存为html的节点。要保存节点的内部html,必须保存每个子节点。
function getHtml($nodes) {
$result = '';
foreach ($nodes as $node) {
$result .= $node->ownerDocument->saveHtml($node);
}
return $result;
}
要获取节点,可以使用Xpath。id很简单
获取所有元素节点:
//*
//*[@id="content"]
只使用第一个找到的节点,以防有人多次添加相同的id。
//*[@id="content"][1]
获取子节点—node()包括元素、文本和其他几个节点
//*[@id="content"][1]/node()
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
echo getHtml($xpath->evaluate('//*[@id="content"][1]/node()'));
class属性稍微复杂一些。类属性是令牌列表,它们可以包含多个类名。这里有一个匹配它们的技巧。Xpath函数normalize-space()将所有空格组转换为单个空格分隔符。在前面和末尾添加一个空格,你会得到一个像" one two three "
这样的字符串。现在您可以检查" one "
是否是该字符串的一部分。在Xpath:
规范类属性:
normalize-space(@class)
开始和结束添加空格:
concat(" ", normalize-space(@class), " ")
检查是否包含子字符串
contains(concat(" ", normalize-space(@class), " "), " title ")
使用它来限制节点
//*[contains(concat(" ", normalize-space(@class), " "), " title ")][1]/node()
放在一起:
$html = <<<'HTML'
<html>
<title></title>
<body>
<h1 class="title">I am title</h1>
<div id="content">
i am the <b>content</b>.
</div>
</body>
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
function getHtml($nodes) {
$result = '';
foreach ($nodes as $node) {
$result .= $node->ownerDocument->saveHtml($node);
}
return $result;
}
// first node with the id
var_dump(
getHtml(
$xpath->evaluate('//*[@id="content"][1]/node()')
)
);
// first node with the class
var_dump(
getHtml(
$xpath->evaluate(
'//*[contains(concat(" ", normalize-space(@class), " "), " title ")][1]/node()'
)
)
);
// alternative - handling multiple nodes with the same class in a loop
$nodes = $xpath->evaluate(
'//*[contains(concat(" ", normalize-space(@class), " "), " title ")]'
);
foreach ($nodes as $node) {
var_dump(getHtml($xpath->evaluate('node()', $node)));
}
输出:https://eval.in/118248
string(40) "
i am the <b>content</b>.
"
string(10) "I am title"
string(10) "I am title"
这很简单。试着
$dom_doc = new DomDocument();
$dom_doc->loadHTML($returned_external_html);
$element = $dom_doc->getElementsByTagName('table'); // you can search for any tags like <img>, <p> and etc. This will return a DOMNodeList
$element = $dom_doc->getElementById('specific_id'); // If you know the id of element you are seeking for try this. This will return a DOMElement
//If I want to getINNERHTML for the table element, the code should be:
$innerHTML= '';
$children = $element->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
echo $innerHTML; //contain the inner html of the element
查看这些链接以获取更多帮助
DOMDocument GetElementsByTagName
DOMDocument GetElementById