0

はい、javascript/jQuery を使用してこれを行うことができることは理解していますが、私は PHP を使用したいと考えています (それはより学習的なことです)。これはクライアントの Web ホスト上にあるため、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 に答える