In javascript, var myStringToArray = myString.split('');
is perfectly acceptable.
But in PHP, $My_String_To_Array = explode('', $My_String);
throws an error:
Warning: explode() Empty delimiter
The PHP manual (http://php.net/manual/en/function.explode.php) explains:
If delimiter is an empty string (""), explode() will return FALSE.
So what should I be using in contemporary PHP instead of explode('', $My_String)
?
So far, the alternatives I can see are:
split("", $My_String)
- deprecated as of PHP 5.3.0str_split($My_String)
preg_split('//', $My_String)
Seven years ago, str_split()
would have been the correct alternative to use.
But is str_split()
still in contemporary usage or should I only be looking at preg_split()
?
Or should I be looking at something else?
explode(' ',$string)
. If you want to split a string into single characters, you can access that by doing$string[0] ... $string[6]
and so on. – Heterotypicstr_split
man page, so yes, it can be used. – Locarnostr_split()
is perfectly valid. It is not deprecated and, most probably, it will never be. Don't mistake it forsplit()
which is just an alias ofexplode()
.preg_split()
is not a replacement forstr_split()
. They have different arguments and work in different ways. – Tiein