[英]Get body id using DOMXpath
是的,我意識到您可以使用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值。
有任何想法嗎?
看來他正在嘗試使一個函數更容易執行某些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.
}
?>
我沒有測試,但是看起來不錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.