Creating seperate variable from string

Hello,

I have a string of data and I need to separate it into individual values.

For example:

$result = "Randy; Mike; John; Cathy";

I need to separate them so each is an individual value in an array.

$result[0] = "Randy";
$result[1] = "Mike";
$result[2] = "John";
$result[3] = "Cathy";

Can i do this with list()?

Thanks

Sure, [fphp]explode/fphp will do this for you. :wink:


<?php
$names = explode(
    '; ',
    'James; Anthony; John; Robin; Joe'
);

print_r(
    $names    
);

/*
    Array
    (
        [0] => James
        [1] => Anthony
        [2] => John
        [3] => Robin
        [4] => Joe
    )
*/
?>

Instead of exploding on "; " (with the space), you might want to explode on only the “;” and the get rid of any whitespace separately.
$names = array_map(‘trim’, $alreadyExplodedArray);

Advantage is that it works no matter much many spaces or whitespace is before/after the “;”.