Page 1 of 1

How to replace binary data in binary files using php

Posted: Mon Dec 06, 2010 12:22 am
by Saman
Use something like below. We can use file_get_contents, str_replace safely on binary strings.
Here on this example we replace hex values 0xA1 with 0x21, 0xA2 with 0x22 and 0xA3 with 0x23.

Code: Select all

$q = file_get_contents($filename);
$data = Array("\xA1" => "\x21", "\xA2" => "\x22", "\xA3" => "\x23");
foreach ($data as $key => $value) {
    $q = str_replace($key, $value, $q);
}