What potential issues can arise when using str_replace() in PHP for binary data manipulation?

When using str_replace() in PHP for binary data manipulation, potential issues can arise due to the function treating the data as strings and potentially altering binary values unintentionally. To solve this issue, it is recommended to use the binary-safe function, str_replace() with the binary-safe version, str_replace_callback().

// Using str_replace_callback() for binary-safe data manipulation
$binaryData = "\x00\x01\x02\x03";
$search = "\x01";
$replace = "\x04";

$result = str_replace_callback($search, function($match) use ($replace) {
    return $replace;
}, $binaryData);

echo bin2hex($result); // Output: 00040203