0

How can I split a string like:

DEFGHIJKLMN

into an array of characters like:

$diskVolume = array('D','E','F','G','H','I','J','K','L','M','N');

explode() doesn't work for this kind of string because there are no delimiters on which to split the string.

1
  • You should check out the "See Also" suggestions in the php manual ("str_split() - Convert a string to an array"). Commented Jan 25, 2011 at 9:47

3 Answers 3

7

Use str_split() to split a string into its individual characters:

$diskVolume = str_split($string);
Sign up to request clarification or add additional context in comments.

Comments

1

You can actually use array access to read from strings:

>> $a = 'CDEFGHIJKLMO';
'CDEFGHIJKLMO'
>> echo $a[2];
E

However you can't use it as an argument for foreach() and similar constructs/functions

1 Comment

Yup, because strings are not arrays.
0

Should use str_split or

for($i=0;$i<strlen($diskVolume);$i++){
echo $diskVolume[$i];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.