PHP: Turn a string into an array of characters

Updated: January 9, 2024 By: Guest Contributor Post a comment

Introduction

Manipulating strings is a fundamental skill in any programming language, and in PHP, there are several ways to turn a string into an array of characters. Doing this allows for more granular control of the string’s content, letting you handle, alter, and analyze data at a character level. Whether you are a beginner or an experienced PHP developer, understanding how to convert strings to arrays will undoubtedly enhance your coding toolbox.

Using str_split

The simplest method to convert a string into an array of characters in PHP is by using the str_split function. This function splits a string into an array, with each array element representing a single character.

$string = "Hello";
$characters = str_split($string);
print_r($characters);

This will output:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

You can also specify a second parameter to str_split to define the chunk length. If you set the chunk length to 2, each array element will contain two characters.

$string = "Hello world";
$characters = str_split($string, 2);
print_r($characters);

This will output:

Array
(
    [0] => He
    [1] => ll
    [2] => o 
    [3] => wo
    [4] => rl
    [5] => d
)

Using str_split with Multibyte Strings

For multibyte (non-ASCII) characters, str_split does not suffice as it may split the characters incorrectly. For such cases, PHP offers the mb_str_split function, which respects the encoding of the characters and properly splits them.

$string = "こんにちは";
$characters = mb_str_split($string);
print_r($characters);

This will output the correct array of multibyte characters:

Array
(
    [0] => こ
    [1] => ん
    [2] => に
    [3] => ち
    [4] => は
)

Using preg_split for Advanced Needs

If you need to split the string based on a regex pattern or if the string is very to delineate with specific indicators, preg_split is a powerful function to achieve this.

$string = "H.e.l.l.o";
$characters = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
print_r($characters);

This will split the string after each character, ignoring dots:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

Using Explode with Delimiters

In some cases, strings might already have delimiters that we can use to convert the string to an array. In such scenarios, the explode function is the best choice. The explode function breaks a string into an array by a string delimiter.

$string = "H,e,l,l,o";
$characters = explode(',', $string);
print_r($characters);

This assumes that the string has commas separating characters:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

Iterating Over Strings

Another approach to convert a string into an array is by iterating over the string character by character and pushing each character to a new array. This method is more manual but allows for custom processing of each character.

$string = "Hello";
$length = mb_strlen($string);
$characters = [];

for ($i = 0; $i < $length; $i++) {
    $characters[] = mb_substr($string, $i, 1);
}

print_r($characters);

Using an iterative approach will result in an array of characters:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

Turning a String into an Associative Array

Sometimes you might want to convert a string into an associative array where each character is a key associated with a certain value. This advanced technique requires mapping each character to a specific value:

$string = "Hello";
$character_counts = array_count_values(str_split($string));
print_r($character_counts);

Here’s what this snippet achieves:

Array
(
    [H] => 1
    [e] => 1
    [l] => 2
    [o] => 1
)

Conclusion

Turning a string into an array of characters in PHP can be done using various approaches, each suiting different scenarios and needs. From simple use cases handled by str_split to more complex patterns managed by preg_split, PHP developers have a range of tools at their disposal. Mastering these methods will make data manipulation and analysis tasks much simpler and more efficient.