Error with unicode and plain xml

I am trying to add a string to an XML object using Simple XML.

Example ( http://ideone.com/L4ztum ):

 $str = "<aoc>   ,  ., - ,  . .12</aoc>";

$movies = new SimpleXMLElement($str);

But this gives a warning:

PHP warning: SimpleXMLElement :: __ construct (): Entity: line 1: parser error: invalid PCDATA Char value 2 in / home / nmo 2E7 / prog.php on line 5

and finally, an exception with a String message cannot be parsed as XML.

If I remove two Unicode characters, it works ( http://ideone.com/LaMvHN ):

$str = "<aoc>   ,  ., - ,  . .12</aoc>";
                          ^
                           `-- two invisible characters have been removed here

How to remove Unicode from a string?

+4
source share
2 answers

, , \x01 \x02. str_replace:

$s = str_replace("\x01", "", $s);
$s = str_replace("\x02", "", $s);
0

SimepleXMLElement , XML.

,

$str = "<aoc> \x02\x01  ,  ., - ,  . .12</aoc>";

XML, XML, :

  • " " (U + 0002) 24
  • " " (U + 0001) 25

, SimpleXMLElement, XML- ( ), XML, . .

, , XML. XML, , , , ( "<aoc>" ).

$text     = '  ,  ., - ,  . .12';
$xml      = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><aoc/>');
$xml->{0} = $text; // set the document-element text-content to $text

SimpleXML , SimpleXMLElement :

$str    = $xml->asXML();
$movies = new SimpleXMLElement($str);
print_r($movies);

/* output:

SimpleXMLElement Object
(
    [0] =>   ,  ., - ,  . .12
)

*/

, :

Unicode ?

Unicode . SimpleXML Unicode ( UTF-8). , Unicode, XML. SimpleXML , node -, .

, - XML (simplexml_load_string ..), () .

, .

0

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


All Articles