Php xpath associated with apostrophe / single quote in the search text

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

basically my code is as follows

$keyword = $_GET['keyword']; ...snip... $xml = simplexml_load_file($data); $search = strtolower($keyword); $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $lower = "abcdefghijklmnopqrstuvwxyz"; $nodes = $xml-xpath("//line[contains(translate(text(),'$upper','$lower'),'$search')]"); 

again, all of the above codes work fine - I can search for strings inside nodes, and I get the correct matches.

However, if the node looks like this: <line number="23">Shall I compare thee to a summer day?</line>

and I'm looking for a summer day ... I get errors in the above line $nodes . What else if I search for "... summer" (no apos), the above line does not match. The only way to return the above string is to search for "... summer", which will include daylight saving time.

I tried stripslashes, addlashes, tohellwithslashes, htmlspecialchars, but nothing works. Also, according to Google, in XPATH 1.0 (which I have to use because it's PHP), I NEVER can escape the apostrophe. Really?

So, I turn to the geniuses here, someone MUST deal with an XML file that they needed to go from XPATH to PHP, which had an apostrap! If XPATH cannot do this, what can I do in PHP to get XPATH to return this node?

0
source share
3 answers

Since Google has shared with you, you cannot escape the apostrophe in XPath. The simplest workaround is to use a different quotation mark around the string parts of the request.

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

Of course, the above is only useful if you do not want to allow double quotes in the search value. If this may be necessary, you can transfer the search / comparison to PHP ground using the method that Gordon pointed out in your previous question .

+3
source

(which I am forced to use since this is PHP)

maybe http://basex.org/api might be worth a look / try. It allows you to use XQuery / XPath and communicates via REST or sockets. In addition, I would recommend solutions for salads.

0
source

Well, I was in the same quest, and after a moment I found that there was no support for xpath for this, quiet disappointment! But we can always get around this!

I needed something simple and straightforward. I came in order to install my own replacement for the apostrophe , a kind of unique code (something that you will not encounter in your XML text), I chose // apos // for example. Now you put this in both your XML text and your xpath request . (in the case of xml, you did not always write, we can replace the replacement function of any editor). Now how do we do it? we usually search this, extract the result, and replace //// apos // with '.

some examples from what i was doing:

  function repalce_special_char_xpath($str){ $str = str_replace("//apos//","'",$str); /*add all replacement here */ return $str; } function xml_lang($xml_file,$category,$word,$language){ //path can be relative or absolute $language = str_replace("-","_",$language);// to replace - with _ to be able to use "en-us", ..... $xml = simplexml_load_file($xml_file); $xpath_result = $xml->xpath("${category}/def[en_us = '${word}']/${language}"); $result = $xpath_result[0][0]; return repalce_special_char_xpath($result); } 

text in XML file:

 <def> <en_us>If you don//apos//t know which server, Click here for automatic connection</en_us> <fr_fr>Si vous ne savez pas quelle serveur, Cliquez ici pour une connexion automatique</fr_fr> <ar_sa>إذا لا تعرفوا أي سرفير, إضغطوا هنا من أجل إتصال تلقائي</ar_sa> </def> 

and call in php file (generated html):

 <span><?php echo xml_lang_body("If you don//apos//t know which server, Click here for automatic connection")?> 
0
source

Source: https://habr.com/ru/post/1332238/


All Articles