Help - Search - Member List - Calendar
Full Version: break string into an array
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
Exyle
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
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
Geoff Berrow wrote:
QUOTE
Strings are already arrays.


If this where true, the $string[0] syntax would not have been deprecated.


JW

Janwillem Borleffs
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
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
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
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.


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.