In PHP programming, we often need to convert strings into byte arrays. In this article, we will introduce how to convert characters into byte array in PHP.
1. Use str_split() function
PHP built-in function str_split() can split a string into a character array. This function passes a string as the first parameter, and the second parameter specifies the length of each group. If the second parameter is not specified, the default length is 1. The following is an example of converting a string into a byte array:
$str = "Hello World"; $bytes = str_split($str); print_r($bytes);
The output result is as follows:
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => W [7] => o [8] => r [9] => l [10] => d )
We can see that the str_split() function has successfully converted the string into Byte array.
2. Use the unpack() function
PHP built-in function unpack() can divide the binary string into different variables. We can use this function to convert a string into a byte array. The following is an example:
$str = "Hello World"; $bytes = unpack('C*', $str); print_r($bytes);
The output is as follows:
Array( [1] => 72 [2] => 101 [3] => 108 [4] => 108 [5] => 111 [6] => 32 [7] => 87 [8] => 111 [9] => 114 [10] => 108 [11] => 100 )
We can see that the unpack() function has successfully converted the string into a byte array.
3. Use the mb_str_split() function
If the string we process contains multi-byte characters, we can use the mb_str_split() function to convert the string into a byte array. The following is an example:
$str = "你好世界"; $bytes = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY); foreach($bytes as &$byte) { $byte = bin2hex(mb_convert_encoding($byte, 'UCS-2BE')); } print_r($bytes);
The output is as follows:
Array ( [0] => 4f60 [1] => 597d [2] => 4e16 [3] => 754c )
We can see that the mb_str_split() function has successfully converted a string containing multi-byte characters into a byte array .
The above are three methods of converting characters into byte arrays in PHP. We can choose different methods to implement this function according to our needs.
The above is the detailed content of How to convert php characters into byte array. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
