0

是的,我意识到你可以使用 javascript/jQuery 来做到这一点,但我想使用 PHP(这更像是一种学习的东西)。我无法安装 queryPath 或 phpQuery,因为它位于客户端的虚拟主机上。

基本上我正在修改

    function getElementById($id) {
    $xpath = new DOMXPath($this->domDocument);
    return $xpath->query("//*[@id='$id']")->item(0);
}

使用,但是

Fatal error: Using $this when not in object context in blahblahblah on line #

被抛出并且$this未定义。

基本上我想要做的是获取 PHP 所在页面的 body id 值。

有任何想法吗?

4

1 回答 1

0

看起来他正试图制作一个函数来更容易地完成一些 xpath 的事情。

就像是

<?php
function getElementById($id,$url){

$html = new DOMDocument();
$html->loadHtmlFile($url); //Pull the contents at a URL, or file name

$xpath = new DOMXPath($html); // So we can use XPath...

return($xpath->query("//*[@id='$id']")->item(0)); // Return the first item in element matching our id.

}

?>

我没有测试这个,但它看起来是正确的。

于 2012-04-19T07:02:51.740 回答