Breaking up a string char by char

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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Breaking up a string char by char

Post by pickle »

Hi all,

I've got a string of arbitrary length and character makeup. I need/want to break that string up into an array, with each element being a character. I know PHP 5 has that functionality but alas, I'm only running PHP 4.3.something. Does anyone know of a simple way of doing this? Thanks muchly.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
$str = 'sdfdfsdfsdfd';
$size = strlen($str);
for($x = 0; $x<$size; $x++)
{
$letters[] = $str{$x};
}
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Awesome. Haven't tried it yet, but it looks like it'll work. Could you explain what this does exactly? I've heard of dynamically naming variables using {}, but how does this work? Thanks.

Code: Select all

$letters[] = $str{$x};
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

goes through and adds the xth character of the string to the array letters
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

yep, if you know how long the string would be, then there is no need for the FOR loop, u can simply do.

$str{0}.$str{1}
and so on :).
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

You could add

Code: Select all

echo "Letter $x:  letters[$x]"."<br>";
To output them, right?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Steveo31 wrote:You could add

Code: Select all

echo "Letter $x:  letters[$x]"."<br>";
To output them, right?
well no, unless you want to do another FOR loop for $letters array, you will need to use this...

Code: Select all

<?php
$str = 'sdfdfsdfsdfd'; 
$size = strlen($str); 
for($x = 0; $x<$size; $x++) 
{ 
echo 'Letter '.$x.': '.$str{$x}.'<br />';
}
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or just

Code: Select all

$str = 'string'; 
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); 
print_r($chars);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

qads wrote:

Code: Select all

<?php
$str = 'sdfdfsdfsdfd';
$size = strlen($str);
for($x = 0; $x<$size; $x++)
{
$letters[] = $str{$x};
}
?>
That didn't work. $letters manages to be the correct size, but it's an empty array.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Like quads implied above, a string is already (in effect) an array of characters, so with
$str = 'hello';
then $str{2} is 'l', $str{0} is 'h' etc..not sure why you want to copy it into another array as each char is accessable via $str{0} to $str{strlen($str)-1}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

When I started on this endeavor, I didn't realize how easy strings were to traverse, so I didn't know what function calls I'd need to make. I wanted to make the for loop I use later as simple as possible, so I wanted a simple array to deal with, and not a possible function call.

Since I had only a few minutes to get this thing to work (it was taking down a campus-wide service), I abandoned using $str{$x} for something I was more familiar with:

Code: Select all

$size = strlen($info);
for($index = 0;$index < $size: $index++)
{
  $chars[] = substr($info,$index,1);
}
Thanks for all the help though guys.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Weirdan wrote:or just

Code: Select all

$str = 'string'; 
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); 
print_r($chars);
Weirdan, do you have a tutorial, reference guide or other learning resource to suggest to the one who wants to gain or improve knowledge on regexp?

Scorphus.
Post Reply