I want to write a string into an XML node, but I have to strip any forbidden characters before doing so. I found the following piece to work:
preg_replace("/[^\\x0009\\x000A\\x000D\\x0020-\\xD7FF\\xE000-\\xFFFD]/", "", $var)
However, it removes alot of characters that I want to keep. Such as space
, ;
, &
, <
, >
\
, and /
.
I did some searching and found space to be x0020
so I tried first to allow spaces by changing the above code to:
preg_replace("/[^\\x0009\\x000A\\x000D\\x0021-\\xD7FF\\xE000-\\xFFFD]/", "", $var)
but it still removes spaces. I just want to remove those weird hidden "command" characters. How can I do that?
EDIT: I have previously made $var
with htmlspecialchars()
, hence why I want to keep &
and ;