Convert a String into an Array of Characters in PHP
Last Updated :
23 Jul, 2025
Given a string, the task is to convert the string into an array of characters using PHP.
Examples:
Input: str = "GFG"
Output: Array(
[0] => G
[1] => f
[2] => G
)
Input: str = "Hello Geeks"
Output: Array(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => G
[7] => e
[8] => e
[9] => k
[10] => s
)
There are two methods to convert strings to an array of characters, these are:
Using str_split() Function
The str_split() function is used to convert the given string into an array. This function basically splits the given string into smaller strings of length specified by the user and stores them in an array and returns the array.
Syntax:
array str_split($org_string, $splitting_length)
Example:
PHP
<?php
$str1 = "Geeks";
print_r(str_split($str1));
$str2 = "Welcome GfG";
print_r(str_split($str2));
?>
OutputArray
(
[0] => G
[1] => e
[2] => e
[3] => k
[4] => s
)
Array
(
[0] => W
[1] => e
[2] => l
[3] => c
[4] => o
[5] => m
[6] => e
[7] =>
[8] => G
... Using preg_split() Function
The preg_split() function is used to convert the given string into an array. The function splits the string into smaller strings or sub-strings of length which is specified by the user.
Syntax:
array preg_split( $pattern, $subject, $limit, $flag )
Example:
PHP
<?php
$str1 = "Geeks";
print_r(preg_split('//', $str1 , -1, PREG_SPLIT_NO_EMPTY));
$str2 = "Welcome GfG";
print_r(preg_split('//', $str2 , -1, PREG_SPLIT_NO_EMPTY));
?>
OutputArray
(
[0] => G
[1] => e
[2] => e
[3] => k
[4] => s
)
Array
(
[0] => W
[1] => e
[2] => l
[3] => c
[4] => o
[5] => m
[6] => e
[7] =>
[8] => G
... Using mb_str_split() Function
The mb_str_split() function is used to convert a multibyte string to an array of characters. This function is useful for handling multibyte encodings such as UTF-8.
Syntax:
array mb_str_split($string, $length, $encoding)
Example:
PHP
<?php
$str1 = "Geeks";
print_r(mb_str_split($str1));
$str2 = "Welcome GfG";
print_r(mb_str_split($str2));
?>
Output:
Array
(
[0] => G
[1] => e
[2] => e
[3] => k
[4] => s
)
Array
(
[0] => W
[1] => e
[2] => l
[3] => c
[4] => o
[5] => m
[6] => e
[7] =>
[8] => G
[9] => f
[10] => G
)
Using array_map() with str_split()
The array_map() function is used to apply a callback function to each element of an array. We can use it in combination with the str_split() function to convert the string into an array of characters.
Example: In this example, we will use the array_map() with str_split()
PHP
<?php
$str1 = "Geeks";
print_r(array_map('strval', str_split($str1)));
$str2 = "Welcome GfG";
print_r(array_map('strval', str_split($str2)));
?>
OutputArray
(
[0] => G
[1] => e
[2] => e
[3] => k
[4] => s
)
Array
(
[0] => W
[1] => e
[2] => l
[3] => c
[4] => o
[5] => m
[6] => e
[7] =>
[8] => G
... Using a Loop
By manually iterating over the string with a loop, you can create an array where each element is a character from the string.
Example
PHP
<?php
function convertStringToArrayLoop($str) {
$arr = [];
for ($i = 0; $i < strlen($str); $i++) {
$arr[] = $str[$i];
}
return $arr;
}
// Examples
print_r(convertStringToArrayLoop("GFG")); // Output: Array([0] => G [1] => F [2] => G)
print_r(convertStringToArrayLoop("Hello Geeks")); // Output: Array([0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => G [7] => e [8] => e [9] => k [10] => s)
?>
Output:
Array
(
[0] => G
[1] => F
[2] => G
)
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => G
[7] => e
[8] => e
[9] => k
[10] => s
)
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance