Re: str_replace binary safe??
| From: | Oliver Heinisch | Date: | Mon, 22 Oct 2001 21:00:05 +0000 |
| Subject: | Re: str_replace binary safe?? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71869@lists.php.net to get a copy of this message | ||
At 22.10.01 21:36, you wrote:
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?? 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...Thanks DLNeil, I tried "rb" "r" "r+b"(C-style) always the same :( Any other suggestions ?