Forum Moderators: coopster
<?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;
Someone mentioned using unserialize but their native language isn't English. Suggestions please?
- John
$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0';
$pieces = explode('_', $cookie);
//print_r($pieces);foreach($pieces as $key=>$value) {
$value = explode('.', $value);
//print_r($value);
echo $value[0] .' has the value '. $value[1] .'<br>';
}
the print_r's are commented out but show the make up of the arrays
I've actually decided it would be easier to pursue the goal a different way and all I need to get it to work is a regex filter that matches a string *after* a period. The new thread is located at...
[webmasterworld.com...]
I'd still consider answers to this question though I feel I'm much closer to a working solution in the newer thread.
- John
However before we shake hands and eat cake I want to understand what the heck ${$value[0]} is referenced as?
I've modified the script a little to make it crystal clear that the desired effect exists for those who follow in our finger-types...
- John
<?php
$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0';
$pieces = explode('_', $cookie);foreach($pieces as $key=>$value) {
$value = explode('.', $value);${$value[0]} = $value[1];
echo '$'.$value[0] .' = '. $value[1] .'<br>';
}// so echo $audio should = '0' :-)
echo $audio;
?>
The braces are needed because $value is an array. If it were a scalar, e.g.
$vname = $value[0];
$$vname = $value[1];
would work.
Thanks again and enjoy cake. ;)
- John
So I'm not sure if this will have an adverse effect because if I leave it as '0' it at least seams to work fine. So I'm very interested in your opinions about this one! :D
- John
$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0';
$pieces = explode('_', $cookie);foreach($pieces as $key=>$value)
{
$value = explode('.', $value);if (empty($value[1])) {$value[1] = '0';}
else {
${$value[0]} = $value[1];
}
echo '$'.$value[0] .' = '. $value[1] ."\n";
}