K
K
Kripto772018-10-03 12:06:39
PHP
Kripto77, 2018-10-03 12:06:39

How to clean xml file from invalid characters?

An xml file comes to the site from another program, which often contains a string PHP cannot parse the file - it gives an error Warning: simplexml_load_string(): Entity: line 34365: parser error : xmlParseCharRef: invalid xmlChar value 4 in I try to clean it up before parsing:
<UNITS>&#4; Not Applicable</UNITS>

$str =  file_get_contents($file_xml);
$str = str_replace('&#4;', '', $str);
$xml = simplexml_load_string($str);

But these characters stay in the string

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-10-03
@Kripto77

Most likely the problem is that the file encoding is different from yours, read here:
www.cyberforum.ru/php-beginners/thread707907.html

K
Kripto77, 2018-10-04
@Kripto77

Maxim You were right - you slipped the file in UCS-2 encoding instead of UTF-8.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question