How to replace binary data in binary files using php

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to replace binary data in binary files using php

Post by Saman » Mon Dec 06, 2010 12:22 am

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);
}
 
Post Reply

Return to “PHP & MySQL”