如何在XPath评估之前处理字符串中的双引号?

Sco*_*t B 4 php string quotes xpath xpath-1.0

在下面的函数中,当$ keyword中的字符串包含双引号时,它会创建一个"警告:DOMXPath :: evaluate():无效的表达式":

$keyword = 'This is "causing" an error';
$xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])');
Run Code Online (Sandbox Code Playgroud)

我该怎么做准备$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.'")])');
        }
}   
Run Code Online (Sandbox Code Playgroud)

hak*_*kre 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".')
Run Code Online (Sandbox Code Playgroud)

辅助函数:

/**
 * 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("'" => '\', "\'", \'')) . "')";
}
Run Code Online (Sandbox Code Playgroud)


Gum*_*mbo 4

要转义XPath 2.0 字符串文字中的字符串分隔符,您需要将每个分隔符替换为两个,因此"需要替换为""

\n\n
\n
[74]      StringLiteral      ::=      (\'"\' (EscapeQuot | [^"])* \'"\') | ("\'" (EscapeApos | [^\'])* "\'") /* ws: explicit */\n[75]      EscapeQuot     ::=      \'""\'\n[76]      EscapeApos     ::=      "\'\'"\n
Run Code Online (Sandbox Code Playgroud)\n
\n\n

I\xe2\x80\x99m 不确定是否已经有一个函数可以做到这一点,但你可以使用这个函数:

\n\n
function xpath_quote($str, $quotation=\'"\') {\n    if ($quotation != \'"\' && $quotation != "\'") return false;\n    return str_replace($quotation, $quotation.$quotation, $str);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

以及用法:

\n\n
\'boolean(/html/body//\'.$heading.\'[contains(.,"\'.xpath_quote($keyword).\'")])\'\n
Run Code Online (Sandbox Code Playgroud)\n

  • `DOMXPath` 是 Xpath 1.0,您链接了 Xpath 2.0 规范。 (4认同)