PHP: Split a string in to an array foreach char PHP: Split a string in to an array foreach char php php

PHP: Split a string in to an array foreach char


You can access characters in strings in the same way as you would access an array index, e.g.

$length = strlen($string);$thisWordCodeVerdeeld = array();for ($i=0; $i<$length; $i++) {    $thisWordCodeVerdeeld[$i] = $string[$i];}

You could also do:

$thisWordCodeVerdeeld = str_split($string);

However you might find it is easier to validate the string as a whole string, e.g. using regular expressions.


you can convert a string to array with str_split and use foreach

$chars = str_split($str);foreach($chars as $char){    // your code}


You can access a string using [], as you do for arrays:

$stringLength = strlen($str);for ($i = 0; $i < $stringLength; $i++)    $char = $str[$i];