search
HomeBackend DevelopmentPHP ProblemHow to convert string to byte array in php

As a high-level programming language, PHP has many powerful functions and features, among which string operation is one of the common functions. In actual development, we often need to convert strings into byte arrays to facilitate some binary data operations. This article will focus on how to convert a string into a byte array in PHP.

1. First of all, you need to clarify what a byte array is.

In computers, bytes are a basic unit of measurement, which can measure the size of binary data. The byte array, as the name suggests, is a data structure composed of multiple bytes. In PHP, byte arrays are usually operated using byte array functions, including obtaining the length of byte arrays, intercepting, merging, etc. Before performing byte array operations, the string needs to be converted into a byte array.

2. Use PHP built-in functions to convert a string into a byte array

1. Use the str_split function

The str_split function can convert a string into a character array. Because a character is actually a byte, this is equivalent to converting a string into a byte array. The syntax is as follows:

array str_split ( string $string [, int $split_length = 1 ] )

Among them, $string represents the string to be converted, $split_length represents the length of each byte array element, and the default is 1.

For example:

$str = 'Hello World';
$bytes = str_split($str);
print_r($bytes);

Output:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => W
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)

As you can see, we successfully converted the string into an array containing multiple bytes.

2. Use the unpack function

The unpack function is a powerful function in PHP that can parse binary data into an array according to the specified format. We can use this function to convert a string into a byte array. The syntax is as follows:

array unpack ( string $format , string $data )

Among them, $format represents the format of the data, and $data represents the binary data to be parsed.

For example:

$str = 'Hello World';
$bytes = unpack('C*', $str);
print_r($bytes);

Output:

Array
(
    [1] => 72
    [2] => 101
    [3] => 108
    [4] => 108
    [5] => 111
    [6] => 32
    [7] => 87
    [8] => 111
    [9] => 114
    [10] => 108
    [11] => 100
)

As you can see, we also successfully converted $string into a byte array.

3. Summary

This article mainly introduces how to convert a string into a byte array in PHP. We can use the str_split function or the unpack function to achieve this. No matter which method is used, you can end up with an array containing multiple bytes, which is convenient for subsequent byte array operations. In actual development, this function is relatively common. I hope this article can provide some help.

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft