xpath - PHP DOMXPath works with double quotes fails with single quotes -


I have written a small script that can be used by PHP's DOMXPath class information from the Web site Removes.
I & lt; Div class = "sku" /> I query for and execute a substring-before result on the result include text, non-breaking spaces, a line break, and more text.
So what I am trying to do is & amp; Nbsp; & Amp; Nbsp; \ R \ n . When I use the following query it works fine:

  $ query = "substring-before (// div [@ class = 'sku'], '\ xC2 \ xA0 \ XC2 \ xA0 \ r \ n ') ";   

But as soon as I keep changing the quote (which should not make any difference):

  $ query = 'substring-before (// div [@ Class = "sku"], "\ xC2 \ xA0 \ xC2 \ xA0 \ r \ n") ';   

or

  $ query = 'substring-before (// div [@class = \' sku \ '], \' \ xC2 \ xA0 \ XC2 \ xA0 \ r \ n \ ')';   

How is this possible and how can I remove it?

Live example here:

The style of citations make a difference Because - in which you are using non-breaking space for \ xC2 \ xA0 , carriage return \ r , and newline \ n .

When they are attached to single-quotes '\ xC2 \ xA0 \ r \ n' , as in your other two questions, PHP treats them as those letters. Does - backslash, x, c, 2 ... etc


A little extra syntax highlighting can help show this show, escape sequence in orange:

 Enter image details here


If you already have a string, then the scenes that will be running in it will be in the form of literal characters , And no way to correct it * , you are in a somber mood of changing yourself.

This preg_replace_cal will take care of sorted sequences in your example, and is trivial to expand in the remaining part supported by double quotes:

  // Good known. $ Query1 = "substring- first (// div [@ class = 'sku'], '\ xC2 \ xA0 \ xC2 \ xA0 \ r \ n')"; / Known bad $ Query2 = 'substring- first (// div [@class = \' sku \ '], \' \ xC2 \ xA0 \ xC2 \ xA0 \ r \ n \ ');'; $ Query2 = preg_replace_callback ('/ \\\\ (?: [RN] | (?: X [0- 9A-FA-F] {1,2}) /', function ($ matches) {switch ( Substant ($ matches [0], 0, 2)) {case '\ r': return "\ r"; case '\ n': return "\ n"; case '\ x': return hex2bin (substr ($ Matches [0], 2));}}, $ query2); Var_dump ($ query1 === $ query2); // now equals?   

Output:

  bool (true)   

(* actually In, you should decide it on the source.)

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -