How to convert byte array to string in php

PHPz
Release: 2023-04-24 16:10:39
Original
1213 people have browsed it

In PHP development, we often need to convert a byte array into a string type. This process is not difficult to implement, but some details need to be paid attention to.

In PHP, byte arrays are actually strings, but when converting them into ordinary strings, some special encoding methods need to be used, such as utf8, gbk, etc.

The following are some commonly used methods for converting byte arrays to strings.

  1. Convert byte array to utf8 string

utf8 is a universal character encoding method commonly used to transmit and save text data on the Internet. In PHP, we can use the mb_convert_encoding function to convert the byte array into a utf8-encoded string. The sample code is as follows:

$byte_array = [228, 185, 139, 229, 173, 151, 229, 156, 168]; // 这是一个 utf8 编码的字符 "中"

$str = join(array_map("chr", $byte_array)); // 将 byte 数组转换成字符串
$str = mb_convert_encoding($str, "UTF-8", "auto"); // 将字符串转换成 utf8 编码
echo $str; // 输出:中
Copy after login

In the above code, we first convert the byte array into a string, and then use the mb_convert_encoding function to convert the byte array into a string. Convert the string to utf8 encoding. The third parameter "auto" here means automatically detecting the encoding method of the current string.

  1. Convert the byte array into a gbk string

gbk is a Chinese encoding method and a commonly used encoding method. In PHP, we can use the iconv function to convert the byte array into a gbk-encoded string. The sample code is as follows:

$byte_array = [66, 118, 75, 49, 50, 52, 216, 247]; // 这是一个 gbk 编码的字符串 "BvK124嘉"

$str = join(array_map("chr", $byte_array)); // 将 byte 数组转换成字符串
$str = iconv("GBK", "UTF-8", $str); // 将字符串转换成 utf8 编码
echo $str; // 输出:BvK124嘉
Copy after login

In the above code, we first convert the byte array into a string, and then use the iconv function to convert the byte array into a string. The string is converted from gbk encoding to utf8 encoding.

  1. Convert a byte array into a base64-encoded string

Sometimes we need to convert a byte array into a base64-encoded string for use in network transmission. In PHP, we can use the base64_encode function to convert the byte array into a base64-encoded string. The sample code is as follows:

$byte_array = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]; // 这是一个普通的 byte 数组

$str = join(array_map("chr", $byte_array)); // 将 byte 数组转换成字符串
$str = base64_encode($str); // 将字符串转换成 base64 编码
echo $str; // 输出:aGVsbG8gd29ybGQ=
Copy after login

In the above code, we first convert the byte array into a string, and then use the base64_encode function to convert the byte array into a string. Convert the string to base64 encoding.

Summary:

In PHP, we can use different functions to convert byte arrays into different string types, such as utf8, gbk, base64, etc. It should be noted that when performing string encoding conversion, you must ensure that the encoding method of the original byte array is consistent with the encoding method of the target string, otherwise it will cause some unnecessary problems.

The above is the detailed content of How to convert byte array to string in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template