How to replace the content in the binary file received by php

Source: Internet
Author: User
Tags printable characters
How to replace the content in the binary file received by php:
Header ('content-type: text/html; charset = utf-8 ');
Error_reporting (0 );
$ Filename = $ _ GET ["filename"];
$ Filesize = $ _ GET ["filesize"];

$ Xmlstr = $ GLOBALS [HTTP_RAW_POST_DATA]; // $ _ POST ["data"]; //
If (empty ($ xmlstr) $ xmlstr = file_get_contents ('php: // input ');

$ Raw = $ xmlstr; // Obtain the binary raw data from post.
$ File = fopen ("./upload/". $ filename, "w"); // open the file to prepare for writing
Fwrite ($ file, $ raw); // write
Fclose ($ file); // Close
?>

The bytes of the non-standard character (> 0x7F) in the received binary file are replaced with three bytes. for example, E2 is replaced with EF 9F A2.
Now I want to restore the file after receiving it in Php and replace EF 9F A2 with E2. how can I implement this?

Thank you very much!


Reply to discussion (solution)

Didn't you receive an XML stream?
It's a text file, not a binary file.
> Aren't 0x7F UTF-8 wide characters?

You 'd better paste the $ raw content.

Fopen ("./upload/". $ filename, "w B ");
Try

Supplement
UTF-8 EF 9F A2 should be unicode F7E2 instead of E2
And the E000-F8FF belongs to the user-defined characters (that is, there is no character set, given to the user area)
For example, some communication interfaces use custom characters for border recognition.

Pay attention to this

The format of the sender is set as follows:
If (xmlhttp. overrideMimeType ){
Xmlhttp. overrideMimeType ('text/plain; charset = x-user-defined ');
} Else {
Xmlhttp. setRequestHeader ('Accept-charset', 'X-user-defined ');
}
Xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset = utf-8 ");

Send and receive files:

You are sending a binary stream.
According to XML conventions, it should be transmitted in base64 encoding
Otherwise, there will be problems in text transmission.

Whether it can be declared as binary data during transmission. you need to check the data

Find this method on the Internet:
XMLHttpRequest. prototype. sendAsBinary = function (datastr ){
Function byteValue (x ){
Return x. charCodeAt (0) & 0xff;
}
Var ords = Array. prototype. map. call (datastr, byteValue );
Var ui8a = new window. Uint8Array (ords );
This. send (ui8a. buffer );
}
Xmlhttp. sendAsBinary (content );

You can use the IE browser on the PC.
However, I need to use the android browser. this function Uint8Array is not supported, so I don't know how to use binary stream to send it.

The comparison between sending and receiving files is still regular:
If the prefix is greater than C0, EF 9F is used. for example, E2 is replaced with EF 9F A2.
The prefix less than C0 is EF 9E. for example, B2 is replaced with EF 9E B2.

I don't know how this encoding came out? So I want to replace the hexadecimal format of the received file, as long as the result is correct.

Is there any way to replace the binary number?
Can regular expressions be supported?

Str_replace and strtr can both be used. Regular expressions can also be used.
For example
$ S = str_replace ("\ xEF \ x9E \ xB2", "\ xB2", $ s );

However
If the prefix is greater than C0, EF 9F is used. for example, E2 is replaced with EF 9F A2.
What are the rules?

Its data conversion rules are as follows:
The file header remains unchanged, and the length of the file header is unclear.
<= 7F bytes, because UTF-8 is also the same, it can be considered that UTF-8 is also converted
> = 80 bytes. add F7 at the top and convert it to UTF-8. for example, E2 is converted to F7E2 and then UTF-8 is converted to EF 9F A2.

At least E2/FB/91/B2/81... in this figure satisfy this rule.

The figure below shows the sqlite database file. Obviously, all the data in the database can be stored. the byte value is 0x00-0xff.
If it can be confirmed as stated in #10, write a function.
Algorithm testing

$ S = <TXT00 2D EF 8F A2 1A 05 00 00 00 01 03 EF 9F BB 0000 00 00 00 0E 03 EF 9F BB 02 6B 01 EF 9E 91 02 3600 EF 9E B2 01 60 00 EF 9E 81 00 EF 9E 81 00 2 FTXT; $ s = preg_replace ("/[\ r \ n]/", '', $ s); $ s = pack ('H * ', $ s ); // Construct binary data $ m = 0; for ($ I = 0; $ I
 
  
00 2D E2 1A 05 00 00 00 01 03 FB 00 00 00 00 0E
  
03 FB 02 6B 01 91 02 36 00 B2 01 60 00 81 00 81
00 2F

Str_replace and strtr can both be used. Regular expressions can also be used.
For example
$ S = str_replace ("\ xEF \ x9E \ xB2", "\ xB2", $ s );

However
If the prefix is greater than C0, EF 9F is used. for example, E2 is replaced with EF 9F A2.
What are the rules?

If the prefix is greater than C0 and EF 9F A2 is replaced with A2 + 40, how can this regular expression be written?

This can be written using regular expressions.
Assume that the data already exists in the variable $ s

$s = preg_replace_callback('/[\xef]../', 'foo', $s);function foo($r) {  $c = (ord($r[0]{1}) & 0x03) << 6;  $c += (ord($r[0]{2}) & 0x3f);  return chr($c);}

The figure below shows the sqlite database file. Obviously, all the data in the database can be stored. the byte value is 0x00-0xff.
If it can be confirmed as stated in #10, write a function.
Algorithm testing

$ S = <TXT00 2D EF 8F A2 1A 05 00 00 00 01 03 EF 9F BB 0000 00 00 00 0E 03 EF 9F BB 02 6B 01 EF 9E 91 02 3600 EF 9E B2 01 60 00 EF 9E 81 00 EF 9E 81 00 2 FTXT; $ s = preg_replace ("/[\ r \ n]/", '', $ s); $ s = pack ('H * ', $ s ); // Construct binary data $ m = 0; for ($ I = 0; $ I
   
    
00 2D E2 1A 05 00 00 00 01 03 FB 00 00 00 00 0E
    
03 FB 02 6B 01 91 02 36 00 B2 01 60 00 81 00 81
00 2F

In addition, I would like to ask, the conversion of $ c is printed in the form of characters. if the file is written back in hexadecimal format, what should I do?
I use
$ Raw [$ j ++] = $ c; // raw is the received raw data.
No. Should I switch it again?
I want to use $ raw to directly write files.

Binary data contains a lot of printable characters. to intuitively see them, it is usually printed in one byte and two hexadecimal numbers (such as the software in your Paster)

Converts a hexadecimal string to binary data. generally, pack ('H * ', 'ffffff') is used ')
Php5.4 also provides an hex2bin
Echo hex2bin ("6578616d706c65206865782064617461"); // example hex data

Thank you very much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.