String splitting with no spaces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Crew
Forum Newbie
Posts: 16
Joined: Fri Jul 15, 2005 4:05 am

String splitting with no spaces

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it already is.

Code: Select all

for($i = 0; $i < strlen($str); $i++) {
  echo str_repeat('-',$i).$str[$i]."\n";
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
Post Reply