split a string into an array of characters in php

To split a string into an array of characters in PHP, you can use the str_split() function. This function returns an array of characters, where each character is an element of the array.

Here's an example of how to use str_split() function:

main.php
$string = "Hello, World!";
$chars = str_split($string);

print_r($chars);
74 chars
5 lines

The above code will output the following:

main.php
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] => ,
    [6] =>
    [7] => W
    [8] => o
    [9] => r
    [10] => l
    [11] => d
    [12] => !
)
180 chars
17 lines

In the above code, we first define a string variable $string with a value of "Hello, World!". We then use the str_split() function to split this string into an array of characters, which we store in the $chars variable. Finally, we use the print_r() function to print the contents of the $chars array.

This is a simple and straightforward way to split a PHP string into an array of characters.

gistlibby LogSnag