string to array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Fri Feb 14, 2003 12:39 am

it's already done for you.. no code really. a string is an array of chars so

Code: Select all

<?php
$string = 'length';
for($x=0; $x<strlen($string); $x++)
{
echo $string[$x].'<br>';
}
?>


would have an output of

l
e
n
g
t
h

PaulG
New php-forum User
New php-forum User
Posts: 8
Joined: Wed Nov 27, 2002 12:18 pm
Location: London
Contact:

Postby PaulG » Fri Feb 14, 2003 6:42 am

To take it further and create an array called $arrString:-
<?php
$string = 'length';
for($x=0; $x<strlen($string); $x++)
{
$arrString[$x] = $substr($string, $x, 1);
// you could also do it like this
// $arrString[] = $substr($string, $x, 1);
}
?>


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron