Title: How to remove invalid XML charactes from document with PHP
Tags: php;xml
Question: I trying to generate an XML document which is around 23 to 30 MB, when i open it with Firefox i receive
XML Parsing Error: not well-formed
Location: file:///Users/User/Downloads/export(2).xml
Line Number 137725, Column 1343:
After that I try to validate the document with XML Nanny and I receive the following error:
Invalid Character (Unicode: 0xB)
On several (13) lines: 137725, 137738, 137751, 137764, 137777, 137790, 137803, 137816, 146834, 189949, 193444, 193457, 193470
I've tried several "solutions" which include:
Regular Expression:
preg_replace(
'/[^\x9\xA\xD\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/'
, ' ', $data-&gt;Description);
The problem here is that I'am not quite sure that this is valid RegEx, because I receive Internal Server Error because of enabled mod security in our apache.
I've tried to save my file in UTF-8 with BOM, but that was desperate trying
I've tried to use iconv with 'UTF-8//IGNORE' but and this didn't help
I've tried to use character by character replacement, but this didn't work well with my file because i have 230k lines.. even if i replace the specific tag in which i have that problem i've trigger max_execution_time directive in php and my script is killed.
For now my solution is to clear database records of this invalid characters manually, but this is now proper and correct solution to my problem because in future this script will be used to automate this export and manual editing isn't option or subject of speech.

Here is the accepted answer: I'd first of all stick to the information given by XML Nanny:
Invalid Character (Unicode: 0xB) (several lines)
0xB  is a character from the control character range, but only very limited control characters are allowed in a XML document. I suggest you start replacing those with numerical entities and try again:
$xml = strtr($xml, array("\x0B" =&gt; "&amp;#x0B;"));
Firefox might accept those.
Comment for this answer: Thanks for the answer and correction of my question. Now everything is fine.

Here is another answer: I faced such similar problem sometime back. What I did was used base64encode to encrypt the data before sending it through XML and then after receiving it, I decoded it. Tell me if this works for you. Or if I didnt understand your question clearly?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:04/10/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><xml>', 'Title': 'How to remove invalid XML charactes from document with PHP', 'ViewCount': 2484, 'id': 10095728, 'user_AboutMe': "<p>I'am 23 years old Developer.\nI am born in town of Montana, North West Bulgaria but, i work in Sofia, the capital of Bulgaria.</p>\n", 'user_AccountId': 474553.0, 'user_DisplayName': 'h4cky', 'user_Reputation': 879.0, 'user_Views': 155.0, 'user_id': 884293.0}}