Convert a String into an Array of Characters in PHP
Last Updated :
19 Jul, 2024
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
)
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Use Case Diagram - Unified Modeling Language (UML) A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within
9 min read
Half Wave Rectifier A Half-wave rectifier is an electronic device that is used to convert Alternating current (AC) to Direct current (DC). A half-wave rectifier allows either a positive or negative half-cycle of AC to pass and blocks the other half-cycle. Half-wave rectifier selectively allows only one half-cycle of th
15 min read
What is Agile Methodology? The Agile methodology is a proper way of managing the project with breaking them into smaller phases which is iteration. It basically focus on flexibility of the project which we can change and improve the team work regularly as per requirements.Table of ContentWhat is Agile?What is the Agile Method
14 min read
Top 8 Software Development Life Cycle (SDLC) Models used in Industry Software development models are various processes or methods that are chosen for project development depending on the objectives and goals of the project. Many development life cycle models have been developed to achieve various essential objectives. Models specify the various steps of the process a
9 min read
PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages, making it essential for building data-driven websites. It allows developers to handle database interactions, session
9 min read
How to Download and Install the Google Play Store The Google Play Store is the heartbeat of your Android experienceâhome to millions of apps, games, and updates that keep your device functional, fun, and secure. But what if your phone or tablet doesnât have it pre-installed?In this step-by-step guide, youâll learn how to safely download and install
6 min read
IEEE 802.11 Architecture The IEEE 802.11 standard, commonly known as Wi-Fi, outlines the architecture and defines the MAC and physical layer specifications for wireless LANs (WLANs). Wi-Fi uses high-frequency radio waves instead of cables for connecting the devices in LAN. Given the mobility of WLAN nodes, they can move unr
9 min read
Transaction in DBMS In a Database Management System (DBMS), a transaction is a sequence of operations performed as a single logical unit of work. These operations may involve reading, writing, updating, or deleting data in the database. A transaction is considered complete only if all its operations are successfully ex
10 min read