Page 1 of 1

String splitting with no spaces

Posted: Tue Oct 11, 2005 9:01 am
by Crew
Hi,

A fairly easy one I guess but whats the best way to split

ABCDEFG

so each char is on its own in the array?

Posted: Tue Oct 11, 2005 9:04 am
by feyd
it already is.

Code: Select all

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

Posted: Tue Oct 11, 2005 9:06 am
by Chris Corbyn
Strings are stored like "special" arrays anyway so this should work ;)

Code: Select all

$string = 'foobar';
$letters = array();
for ($x=0; $x<str_length($string); $x++)
{
    $letters[] = $string{$x};
}

print_r($letters);