Home » Php » hex – PHP explode on null character

hex – PHP explode on null character

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am trying to explode a string on what looks like to be a null character.

This is what is what I am using: $exp = explode('\x00', $bin);.

Though this does not work. However, if I do $exp = explode($bin[5], $bin); (where character 5 of $bin is this character I want to explode on) it works fine.

If I do var_dump($bin[5]) It shows me square block with a question mark in it (), and in the view source I get: �

Can anyone tell me what the best way would be to explode on this character? or even if it is the null character (which according to ascii tables it is, unless I am reading it all wrong).

Thanks

How to&Answers:

Try double quotes:

$exp = explode("\x00", $bin);

Alternatively, capture the equivalent ASCII character code and pass it using chr.

$char = ord($bin[5]);

// Replace this with the actual number returned from ord
$exp = explode(chr($char), $bin);

This latter example eliminates the possibility that it might not actually be a null character if you haven’t already determined it.