Exyle
Jul 3 2005, 01:50 AM
I want to break a string up into an array (eg "hello" would end up like
$array[0] = h, $array[1] = e, etc) and I cannot for the life of me
figure out how. things like explode() and split() both require a
seperator which is more advanced than I need in this case. can anyone
point me in the right direction?
Geoff Berrow
Jul 3 2005, 08:24 AM
I noticed that Message-ID: <42c7536b$[Email Removed]> from
Exyle contained the following:
QUOTE |
I want to break a string up into an array (eg "hello" would end up like $array[0] = h, $array[1] = e, etc) and I cannot for the life of me figure out how. things like explode() and split() both require a seperator which is more advanced than I need in this case. can anyone point me in the right direction?
|
Strings are already arrays.
To access individual letters in a string use curly braces instead of
square. e.g.
$string="hello"
print $string{0}."br>";
//prints h
print $string{1}."br>";
//prints e
print $string{2}."br>";
//prints l
....
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Janwillem Borleffs
Jul 3 2005, 11:15 AM
Geoff Berrow wrote:
QUOTE |
Strings are already arrays.
|
If this where true, the $string[0] syntax would not have been deprecated.
JW
Janwillem Borleffs
Jul 3 2005, 11:20 AM
Exyle wrote:
QUOTE |
I want to break a string up into an array (eg "hello" would end up like $array[0] = h, $array[1] = e, etc) and I cannot for the life of me figure out how. things like explode() and split() both require a seperator which is more advanced than I need in this case. can anyone point me in the right direction?
|
Besides Geoff's solution and if you really want to create an array, you can
do the following:
$string = 'hello';
$arr = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
JW
Exyle
Jul 3 2005, 10:18 PM
Thanks guys! I was looking around and managed to figure out another way
which I'll post here incase anyone curious happens to stumble across
this thread:
$string = "Hello";
for($i = 0; $i < strlen($string); $i++) {
$array[$i] = substr($string, $i, 1);
}
Geoff Berrow
Jul 3 2005, 10:38 PM
I noticed that Message-ID: <42c8734a$[Email Removed]> from
Exyle contained the following:
QUOTE |
$string = "Hello";
for($i = 0; $i < strlen($string); $i++) { $array[$i] = substr($string, $i, 1); }
|
<fx: smacks forehead> Of course, that's much easier that using
$string{n}
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Exyle
Jul 3 2005, 11:55 PM
Geoff Berrow wrote:
QUOTE |
I noticed that Message-ID: <42c8734a$[Email Removed]> from Exyle contained the following:
$string = "Hello";
for($i = 0; $i < strlen($string); $i++) { $array[$i] = substr($string, $i, 1); }
<fx: smacks forehead> Of course, that's much easier that using $string{n} LOL, no, thats why I was saying thank you. I found a way on my own but |
yours is quite a bit easier for my purposes.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.