Forum Moderators: coopster

Message Too Old, No Replies

Converting a String to an Array

Possible?

         

ukgimp

3:19 pm on May 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is is possible to do the above mentioned in php. If it is where can I find ut how to do it. Resources?

Cheers

BCMG_Scott

4:13 pm on May 30, 2003 (gmt 0)

10+ Year Member



depends, if the string is delimited in some manner you can use the split function.

Scott Geiger

mavherick

4:20 pm on May 30, 2003 (gmt 0)

10+ Year Member



In itself, a string is after all an array of char. You can access specific indexes of a string like this:

$foo = "hello";

$secondchar = $foo{1};

echo $secondchar;

outputs "e"

I guess you could always loop through it and build your "real" array as you go or simply use this format.

more info on strings [php.net]

[added]From the manual, you can use normal array braces ("[" and "]") for strings but it's deprecated in favor of "{" and "}" as of php 4.[/added]

mavherick

brotherhood of LAN

7:00 pm on May 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I do it pretty much the same way as mavherick says...

$array = array();
$string = "The string to turn into an array";
$len = strlen($string);

for($i = 0;$i < $len;$i++)
{
array_push($array,$string[$i]);
}

print_r($array);

'course it all depends what you want to push into an array, if its a matter of finding words in a sentence, the strtok() function does the job well.

ukgimp

11:16 pm on May 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks people, I will look into that on monday.