Breaking up a string char by char
Moderator: General Moderators
Breaking up a string char by char
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.
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.
Code: Select all
<?php
$str = 'sdfdfsdfsdfd';
$size = strlen($str);
for($x = 0; $x<$size; $x++)
{
$letters[] = $str{$x};
}
?>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
You could add
To output them, right?
Code: Select all
echo "Letter $x: letters[$x]"."<br>";well no, unless you want to do another FOR loop for $letters array, you will need to use this...Steveo31 wrote:You could addTo output them, right?Code: Select all
echo "Letter $x: letters[$x]"."<br>";
Code: Select all
<?php
$str = 'sdfdfsdfsdfd';
$size = strlen($str);
for($x = 0; $x<$size; $x++)
{
echo 'Letter '.$x.': '.$str{$x}.'<br />';
}
?>or just
Code: Select all
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);That didn't work. $letters manages to be the correct size, but it's an empty array.qads wrote:Code: Select all
<?php $str = 'sdfdfsdfsdfd'; $size = strlen($str); for($x = 0; $x<$size; $x++) { $letters[] = $str{$x}; } ?>
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:
Thanks for all the help though guys.
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);
}- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
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?Weirdan wrote:or justCode: Select all
$str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars);
Scorphus.