tags:

views:

1851

answers:

1
+3  A: 
$text = str_replace("\0", "", $text);

will replace all null characters in the $text string. You can also supply arrays for the first two arguments, if you want to do multiple replacements.

Joey
depending on the encoding (utf-8), this could remove valid characters
ax
@Johannes Rössel: I added $text= to make code and text match. str_replace does not modify the third argument
phihag
the code only seems to work when i do this: $text = str_replace("", "", $text);
Benjamin Ortuzar
ax: UTF-8 encodes every character < 128 as the character itself, so if you want to get rid of \0, you should delete \0. No side-effects there. UTF-16 is a different matter entirely, though, but still I'd expect languages to work on strings as strings and not byte arrays, so it's a moot point
Joey
Benjamin: If that works, then your problem are probably not null characters but rather un-encoded ampersands. But I'd expect something that builds an XML node from a string to escape characters properly
Joey