When my $keyword input string in the function below contains double quotes...
'this is a "test" for example'
I'm getting this error:
Warning: DOMXPath::evaluate() [domxpath.evaluate]: Invalid expression
It occurs on the line just below the on开发者_运维问答e commented below "ERROR OCCURS" on the xpath->evaluate method.
function my_function($heading, $post){
$content = $post->post_content;
if($content=="" || !class_exists('DOMDocument')) return false;
$keyword = trim(strtolower(my_getKeyword($post)));
@$dom = new DOMDocument;
@$dom->loadHTML(strtolower($post->post_content));
$xPath = new DOMXPath(@$dom);
switch ($heading)
{//ERROR OCCURS
case "img-alt": return $xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])');
default: return $xPath->evaluate('boolean(/html/body//'.$heading.'[contains(.,"'.$keyword.'")])');
}
}
function my_getKeyword($post)
{
$myKeyword = get_post_meta($post->ID, '_my_keyword', true);
if($myKeyword == "") $myKeyword = $post->post_title;
return " ".$myKeyword;
}
精彩评论