I have a PHP variable that contains a string which represents an XML structure. This string contains ilegal characters that dont let me build a new SimpleXMLElement object f
$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.
$text