Forum Moderators: coopster

Message Too Old, No Replies

Change string of property/values in to variable/values?

         

JAB Creations

12:23 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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;

Someone mentioned using unserialize but their native language isn't English. Suggestions please?

- John

SuzyUK

4:35 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



bear in mind I'm a novice.. but this works for me

$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

JAB Creations

4:47 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you for trying Suzy though I think you missed the point of my goal, to create variables out of the existing strings. I would want to echo $audio and have PHP spit out '0' as I will need these variables by their names later on in a larger script.

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

cameraman

5:01 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SuzyUK's almost got it. In the second loop, add:
foreach($pieces as $key=>$value) {
$value = explode('.', $value);
//print_r($value);
${$value[0]} = $value[1];
echo $value[0] .' has the value '. $value[1] .'<br>';
}

JAB Creations

5:20 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok very awesome! Thanks a huge amount to both of you!

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;
?>

cameraman

5:34 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's just more variable variables [us2.php.net] like you asked about in your other thread [webmasterworld.com].

The braces are needed because $value is an array. If it were a scalar, e.g.
$vname = $value[0];
$$vname = $value[1];
would work.

JAB Creations

5:40 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah! Well I figured there might be a way to use variable variable, however while the idea existed in my head the way of achieving it was too insignificant for me. So I searched through php.net for ideas of how to achieve my goal in other ways and help expand my base understanding of things I can use in PHP.

Thanks again and enjoy cake. ;)

- John

JAB Creations

6:07 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually I do have one more question, I want to set the value of keys with blank values to '0' though with the following code I have a little bit of concern. When I changed it to 0 I decided to see what would happen if I set the value to something different instead such as '777' and then all the values that do exist already as '0' changed to '777' as well!

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";
}

cameraman

6:37 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



empty() evaluates a zero as true. Try checking $value[1] == ''

xiongbin

3:16 am on May 28, 2008 (gmt 0)

10+ Year Member



<?

$cookie =

'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_curso

rs.0';

print_r(explode("_",str_replace(".","=",$cookie)));
?>