Re: str_replace binary safe??
| From: | DL Neil | Date: | Mon, 22 Oct 2001 20:36:14 +0000 |
| Subject: | Re: str_replace binary safe?? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71867@lists.php.net to get a copy of this message | ||
> Hi folks,
> following problem appeared on PHP 3.0.16 on LINUX
> I have a binary file, where I want to replace some letters
> I do the following:
> $filename = "xyz.sdc";
> $fd = fopen( $filename, "r+b" ); // also tried "r"
> $x=filesize( $filename ); // this shows the correct filesize 22528 byte
> $contents = fread( $fd, $x );
> fclose( $fd );
> $y=strlen($contents); // The first mistake $y = 32956 byte
> echo "BEFORE<br>".$contents;
> $contents=str_replace("UUUUUU","123456", $contents);
> echo "AFTER<br>".$contents;
>
> It seems, that php converts HEX 00 00 to HEX 5C 30 just when it reads the
> data in
> memory because at the BEFORE echo the data will be shown as \0 (hex 00 00 ).
> The filesize will be correct when I save the str_replaced text and the text
> will be cahnged
> correct.
> Any idea, where this comes from??
Oliver,
Let's start with the fopen(), where the mode expression might be causing problems - should it
be "rb"?
http://www.php.net/manual/en/function.fopen.php
If the 'binary' component hasn't been recognised it might explain the "first
mistake".
The ensuing problems may even stem from there...
Regards,
=dn