Page 1 of 1

Breaking up a string char by char

Posted: Mon Mar 01, 2004 6:46 pm
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.

Posted: Mon Mar 01, 2004 6:55 pm
by qads

Code: Select all

<?php
$str = 'sdfdfsdfsdfd';
$size = strlen($str);
for($x = 0; $x<$size; $x++)
{
$letters[] = $str{$x};
}
?>

Posted: Mon Mar 01, 2004 7:04 pm
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};

Posted: Mon Mar 01, 2004 7:11 pm
by Illusionist
goes through and adds the xth character of the string to the array letters

Posted: Mon Mar 01, 2004 7:53 pm
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 :).

Posted: Mon Mar 01, 2004 11:42 pm
by Steveo31
You could add

Code: Select all

echo "Letter $x:  letters[$x]"."<br>";
To output them, right?

Posted: Tue Mar 02, 2004 2:48 am
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 />';
}
?>

Posted: Tue Mar 02, 2004 4:40 am
by Weirdan
or just

Code: Select all

$str = 'string'; 
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); 
print_r($chars);

Posted: Tue Mar 02, 2004 10:18 am
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.

Posted: Tue Mar 02, 2004 10:22 am
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}

Posted: Tue Mar 02, 2004 10:51 am
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.

Posted: Tue Mar 02, 2004 11:04 am
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.