...

View Full Version : How do I convert a string to multiple variables?



DeaDLocK
09-12-2009, 08:48 PM
Very simple one guys (I think!).

Let's say I retrieve a $POST from the URL, and the entire value string of the new variable looks like this:

abc_1:def_3:xyz_7

How do I convert this string into three different variables, namely:

abc = 1
def = 3
xyz = 7

It would be easier to have those as separate variables within the URL, I know, but in this instance, I can't - they must be part of the same URL variable, and then I need to split it up into three using PHP.

Thanks!

Eran
09-12-2009, 09:12 PM
explode (http://php.net/manual/en/function.explode.php)

bacterozoid
09-12-2009, 09:13 PM
$results = Array();

$string = $_POST['whatever'];
$temp = explode(":", $string); // Split at the : to get an array of "abc_1", "def_3", "xyz_7"
for($i = 0; $i < count($temp); $i++) {
$temp2 = explode("_", $temp[$i]); // Split at the underscores to get an array like "abc", "1"
$results[$temp2[0]] = $temp2[1];
}

That should make


$results['abc'] = 1;
$results['def'] = 2;
$results['xyz'] = 3;

DeaDLocK
09-12-2009, 09:38 PM
Thank you sir!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum