In PHP development, the pack() function is often used to package data into binary strings, which is very useful when dealing with network protocols, file formats, or underlying data exchange. However, many developers encounter "garbled" problems when outputting data using the pack() function, which is often confusing. This article will analyze the causes of garbled code around this phenomenon and give solutions, especially in-depth discussions from the perspective of character encoding.
The pack() function packages the data into a binary string according to the specified format. Its typical usage is as follows:
<?php
// Package two integers in binary format
$data = pack("CC", 65, 66);
echo $data; // Output AB
?>
Here pack("CC", 65, 66) generates the ASCII characters 'A' and 'B' corresponding to the binary string.
Pack() outputs raw binary data, not necessarily valid text encoding. If you directly use echo or print to output, the terminal or browser will interpret these bytes according to the current default encoding, which may result in garbled code.
<?php
// Pack some binary data
$data = pack("H*", "e4bda0e5a5bd");
echo $data; // This should have been“Hello”ofUTF-8coding,但直接OutputMaybe garbled
?>
Here, e4bda0e5a5bd is the "Hello" UTF-8 hexadecimal encoding. If the environment is not UTF-8 or the encoding is not set correctly, the output will display garbled code.
Different systems or applications have different default processing of encoding. Pack() generates the original bytes and will not automatically convert and encode. If you treat its content as some kind of encoded string, and the actual data encoding does not match, garbled code will inevitably occur.
The output content depends on the encoding and parsing of the receiver. For example, the browser is not UTF-8 by default, or the terminal font does not support the corresponding encoded characters, and garbled code will also be displayed.
Make sure your PHP file is saved as UTF-8 encoding and that your web page or terminal environment also uses UTF-8. You can set it in the webpage:
<?php
header("Content-Type: text/html; charset=utf-8");
?>
If you need to output text to people, first convert the binary data into a suitable encoded string, such as:
<?php
// Pack“Hello”of UTF-8 Hexadecimal data
$data = pack("H*", "e4bda0e5a5bd");
// Output之前用 mb_convert_encoding 转换coding
echo mb_convert_encoding($data, "UTF-8", "UTF-8");
?>
Or use bin2hex() to view the hexadecimal representation of binary data to avoid directly outputting garbled code:
<?php
echo bin2hex($data); // Outpute4bda0e5a5bd,Convenient debugging
?>
Do not blindly convert encoding before sending binary data. Generally speaking, binary data is binary, and the application or protocol layer will be parsed correctly. Only when used for display, it needs to be converted to the corresponding encoded string.
<?php
// by UTF-8 codingPack字符串 “Hello”
$utf8_string = "Hello";
$packed = pack("a*", $utf8_string);
// 直接Output(Maybe garbled)
echo $packed;
// 设置正确coding,Avoid garbled code
header("Content-Type: text/html; charset=utf-8");
// Output转换后of字符串
echo mb_convert_encoding($packed, "UTF-8", "UTF-8");
?>
The essence of the pack() function is to generate binary data, and the garbled problem mostly stems from inconsistent character encoding understanding. The key points are:
Understand that the output of pack() is raw bytes, not ordinary text.
Ensure that data is consistently encoded when output or processed.
Avoid directly outputting binary data as text unless it is correctly encoded.
After mastering these coding principles, you will not easily encounter garbled code problems using pack() .
Domain name example:
<?php
// Example URL Replace the domain name with m66.net
$url = "https://m66.net/api/data";
echo $url;
?>