Log in

View Full Version : Change exploded string of property/values in to variable/values?



JAB Creations
05-24-2008, 12:52 PM
I have a string $cookie that contains properties and values separated by underscores. I can explode the string to get an array of the property and values and below echo them out...


<?php
$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0';
$pieces = explode('_', $cookie);
echo $pieces[0].'<br />';
echo $pieces[1].'<br />';
echo $pieces[2].'<br />';
echo $pieces[3].'<br />';
echo $pieces[4].'<br />';
echo $pieces[5].'<br />';
?>

That will echo out...

audio.0
backgroundimages.
browserpatch.1
chatroom.0
connection.0
css3.0
cursors.0

What I'd like to do is have PHP convert each $piece to a variable and the part after each period to be assigned as that new variable's value.

So the last part above I'd like to essentially look or be in effect like the following...


$audio = 0;
$backgroundimages = ;
$browserpatch = 1;
$chatroom = 0;
$connection = 0;
$css3 = 0;
$cursors = 0;

Suggestions please?

- John

Master_script_maker
05-25-2008, 11:52 AM
<?php
$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0';
$pieces = explode('_', $cookie);
foreach($pieces as $val) {
$contents=explode('.', $val);
$$contents[0]=$contents[1]
}
echo $audio;
echo $backgroundimages;
echo $browserpatch;
echo $chatroom;
echo $connection;
echo $css3;
echo $cursors;
?>

JAB Creations
05-27-2008, 03:12 PM
Excellent! It just does not get any more spot on then that! Thank you very much MSM! :cool

Master_script_maker
05-27-2008, 05:36 PM
you're welcome. always happy to help :)