Open In App

How to read each character of a string in PHP ?

Last Updated : 17 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. 

Here are some approaches to read each character of a string in PHP

Approach 1: Using str_split() method - The str_split() method

Using str_split() method - The str_split() method is used to split the specified string variable into an array of values, each of which is mapped to an index value beginning with 0. This method converts the input string into an array.

str_split(str)

 PHP foreach loop iteration can then be done over the array values, each of which element belongs to a character of the string. The values are then printed with a space in between each. 

Example:

PHP
<?php
  
// Declaring string variable 
$str = "Hi!GFG User.";

echo("Original string : ");
echo($str . "</br>");

$array = str_split($str);
echo("Characters : ");

foreach($array as $val){
    echo($val . " ");
}

?>

Output:

Original string : Hi!GFG User.
Characters : H i ! G F G U s e r .

Approach 2: Using strlen() method - The strlen() method

Using strlen() method - The strlen() method is used to compute the length of the specified string in PHP. A for loop iteration is applied using the length of the string, and the ith index character is printed each time. The time complexity is the same as the previous method. However, no extra space is required to store the string in the form of the array object. 

strlen(str)

Example:

PHP
<?php
  
// Declaring string variable 
$str = "Hi!GFG User.";

echo("Original string : ");
echo($str."</br>");
echo("Characters : ");

// Iterating over the string 
$len = strlen($str);

for ($i = 0; $i < $len; $i++){
    echo ($str[$i]." ");
}

?>

Output:

Original string : Hi!GFG User.
Characters : H i ! G F G U s e r .

Approach 3: Using preg_split()

Using preg_split() in PHP splits a string into an array of characters with a regular expression. By splitting with //u, it handles each character, including multibyte characters, properly. This allows easy iteration through each character in the resulting array.

Example: In this example we splits the string "Hello, World!" into individual characters and prints each character on a new line using a foreach loop with preg_split and Unicode support.

PHP
<?php
$string = "Hello, World!";
$chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);

foreach ($chars as $char) {
    echo $char . "\n";
}
?>

Output
H
e
l
l
o
,
 
W
o
r
l
d
!

Approach 4: Using mb_strlen and mb_substr

Using mb_strlen and mb_substr functions is especially useful for strings with multibyte characters. The mb_strlen function returns the number of characters in the string, and mb_substr is used to retrieve a specific character from the string based on its position.

Example: In this example, we will use mb_strlen and mb_substr method

PHP
<?php

// Declaring string variable 
$str = "Hi!GFG User.";

echo("Original string : ");
echo($str . "\n");
echo("Characters : ");

// Getting the length of the string with mb_strlen
$len = mb_strlen($str, 'UTF-8');

// Iterating over the string using mb_substr
for ($i = 0; $i < $len; $i++) {
    echo (mb_substr($str, $i, 1, 'UTF-8') . " ");
}

?>

Output
Original string : Hi!GFG User.
Characters : H i ! G F G   U s e r . 

Approach 5: Using str_split() with Length Parameter

The str_split() function can be used with an optional second parameter that specifies the length of each chunk. By setting this length to 1, the function effectively splits the string into an array of individual characters.

Example

PHP
<?php

function readCharacters($string) {
    // Split the string into an array of characters
    $characters = str_split($string, 1);
    
    // Print each character with a space in between
    foreach ($characters as $char) {
        echo $char . ' ';
    }
}

$inputString = "Hi!GFG User.";
echo "Original string: " . $inputString . "\n";
echo "Characters: ";
readCharacters($inputString);
?>

Output
Original string: Hi!GFG User.
Characters: H i ! G F G   U s e r . 

Next Article

Similar Reads