在下面的功能,當字符串中的$關鍵字包含雙引號,它創建一個「警告:DOMXPath ::評估():無效的表達式」:如何在XPath評估之前處理字符串中的雙引號?
$keyword = 'This is "causing" an error';
$xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])');
我應該怎麼做來準備$keyword
爲評估XPath表達式?
全功能代碼:
$keyword = trim(strtolower(rseo_getKeyword($post)));
function sx_function($heading, $post){
$content = $post->post_content;
if($content=="" || !class_exists('DOMDocument')) return false;
$keyword = trim(strtolower(rseo_getKeyword($post)));
@$dom = new DOMDocument;
@$dom->loadHTML(strtolower($post->post_content));
$xPath = new DOMXPath(@$dom);
switch ($heading)
{
case "img-alt": return $xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])');
default: return $xPath->evaluate('boolean(/html/body//'.$heading.'[contains(.,"'.$keyword.'")])');
}
}
`DOMXPath`是Xpath的1.0,則鏈接中的XPath 2.0規範。 – hakre 2012-10-10 01:21:00