在下面的函数中,当$ keyword中的字符串包含双引号时,它会创建一个“警告:DOMXPath :: evaluate():无效的表达式”:
$keyword = 'This is "causing" an error';
$xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])');
如何为评估xpath表达式准备$keyword
?
完整的功能代码:
$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.'")])');
}
}
答案 0 :(得分:6)
PHP具有Xpath 1.0,如果您有一个带双引号和单引号的字符串,则解决方法是使用Xpath concat()
函数。辅助函数可以决定何时使用什么。实施例/用法:
xpath_string('I lowe "double" quotes.');
// xpath: 'I lowe "double" quotes.'
xpath_string('It\'s my life.');
// xpath: "It's my life."
xpath_string('Say: "Hello\'sen".');
// xpath: concat('Say: "Hello', "'", "'sen".')
辅助函数:
/**
* xpath string handling xpath 1.0 "quoting"
*
* @param string $input
* @return string
*/
function xpath_string($input) {
if (false === strpos($input, "'")) {
return "'$input'";
}
if (false === strpos($input, '"')) {
return "\"$input\"";
}
return "concat('" . strtr($input, array("'" => '\', "\'", \'')) . "')";
}
答案 1 :(得分:4)
要转义XPath 2.0 string literals中的字符串分隔符,您需要将每个分隔符替换为2,因此"
需要替换为""
:
[74] StringLiteral ::= ('"' (EscapeQuot | [^"])* '"') | ("'" (EscapeApos | [^'])* "'") /* ws: explicit */ [75] EscapeQuot ::= '""' [76] EscapeApos ::= "''"
我不确定是否已有功能可以使用此功能:
function xpath_quote($str, $quotation='"') {
if ($quotation != '"' && $quotation != "'") return false;
return str_replace($quotation, $quotation.$quotation, $str);
}
用法:
'boolean(/html/body//'.$heading.'[contains(.,"'.xpath_quote($keyword).'")])'