Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

string to array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

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);
}
?>
PaulG
New php-forum User
New php-forum User
 
Posts: 8
Joined: Wed Nov 27, 2002 12:18 pm
Location: London


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron