Open In App

How to Iterate Over Characters of a String in PHP ?

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

This article will show you how to iterate over characters of string in PHP. It means how to loop through an array of characters in a string. There are two methods to iterate over the character of a string, these are:

Using str_split() function and foreach Loop

The str_split() function converts the given string into an array and then uses foreach loop to iterate over an array of elements.

Syntax:

$chars = str_split($str);
foreach ($chars as $char) {
echo $char . "\n";
}

Example: This example uses str_split() function and foreach Loop to iterate over characters of string.

PHP
<?php

// Declare an array
$str = "GeeksforGeeks";

$chars = str_split($str);

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

?>

Output
G
e
e
k
s
f
o
r
G
e
e
k
s

Using for Loop

The for loop is used to iterate the array elements and execute the block. The for loop contains the initialization expression, test condition, and update expression (expression for increment or decrement).

Syntax:

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

Example: This example uses for loop to iterate over characters of string.

PHP
<?php

// Declare an array
$str = "GeeksforGeeks";

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

?>

Output
G
e
e
k
s
f
o
r
G
e
e
k
s

Using mb_substr() with while loop

Using mb_substr() with a while loop in PHP involves determining the string length with mb_strlen() and iterating through each character using mb_substr(), ensuring correct handling of multi-byte characters for accurate iteration.

Syntax:

while ($i < $len) {
$char = mb_substr($str, $i, 1, 'UTF-8');
echo $char . "\n";
$i++;
}

Example:

PHP
<?php

$str = "GeeksforGeeks";
$len = mb_strlen($str, 'UTF-8');
$i = 0;

while ($i < $len) {
    $char = mb_substr($str, $i, 1, 'UTF-8');
    echo $char . "\n";
    $i++;
}

?>

Output:

G
e
e
k
s
f
o
r
G
e
e
k
s

Using preg_split() with foreach Loop

In this approach, we utilize the preg_split() function to split the string into an array of characters based on a regular expression pattern. We then use a foreach loop to iterate over each character in the resulting array.

Syntax:

$chars = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) {
// code here...
}

Example:

PHP
<?php

$str = "HelloWorld";

$chars = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) {
    echo $char . "\n";
}
// Nikunj Sonigara
?>

Output
H
e
l
l
o
W
o
r
l
d

Using strpbrk() with While Loop

In this approach, strpbrk() function is used to find the first occurrence of any of the specified characters in a string. By repeatedly removing the first character, we can iterate over each character in the string.

Example:

PHP
<?php
$str = "GeeksforGeeks";
while ($str !== false && $str !== '') {
    $char = $str[0];
    echo $char . "\n";
    $str = substr($str, 1);
}
?>

Output
G
e
e
k
s
f
o
r
G
e
e
k
s


Using strtok with While Loop

Another valid approach to iterate over characters of a string in PHP is by using the strtok function in combination with a while loop. The strtok function tokenizes a string, and by specifying an empty string as the delimiter, it allows us to iterate over each character one by one.

Example:

PHP
<?php
function iterateWithStrtok($str) {
    $token = strtok($str, '');
    while ($token !== false) {
        echo $token . "\n";
        $token = strtok('');
    }
}


$inputString = "GeeksforGeeks";
iterateWithStrtok($inputString);
?>

Output
GeeksforGeeks

Next Article

Similar Reads