php xpath dealing with apostrophe / single quote in searched text

前端 未结 3 1258
既然无缘
既然无缘 2020-12-07 05:53

In my PHP script, I\'m using XPATH to search nodes for text. Everything works swimmingly -except - when I search for a word with an apostrophe.

basically my code lo

3条回答
  •  不知归路
    2020-12-07 06:18

    As Google shared with you, you cannot escape an apostrophe in XPath. The simplest workaround is to use a different quote character around the string parts of the query.

    $nodes = $xml->xpath('//line[contains(translate(text(),"'.$upper.'","'.$lower.'"),"'.$search.'")]');
    

    Of course, the above is only useful if you don't want to allow double-quotes in the search value. If that might be necessary, then you could move the searching/comparison into PHP-land using the method that Gordon pointed out in your previous question.

提交回复
热议问题