Remove \0 null character in PHP with preg_replace in entire string

remove-0-null-character-php-preg_replace

Hey,
if you’re trying to eliminate the \0 null characters in a string (maybe because they are saved in your MySql and echoed like in the screenshot above), after using mysql_real_escape_string() it somehow doesn’t work.

So you first need to run the following preg_replace function in PHP, in order to get rid of the \0 null characters in an entire string:

preg_replace('/\\0/', "", $var);

This is, what worked for me, after hours of research!

Then you might also run mysql_real_escape_string() after, but you don’t have to.

Trim() only removes the first (and last?) occurance.

Have fun.

  1. Finally, this helped me a lot – thank you very much!!!

Leave a Comment