Jump to content


Photo

Convert Variable name into string


  • Please log in to reply
6 replies to this topic

#1 slpwkr

slpwkr
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 01 March 2006 - 10:15 AM

Hey guys is there a function to convert a variable name into string?

Ex:

$variablename= 'whatever';

I want to print the "variablename" only without the $ (dollor) sign and not using a substr() function. thank you!

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 01 March 2006 - 10:27 AM

I'm bit confused with you saying without the $ because aint just just

echo "variablename";

above will read as: variablename

or did you mean actualy echo $variablename e.g.

echo '$variablename';

above will read as: $variablename


Nott 100% on what you mean?
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 slpwkr

slpwkr
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 01 March 2006 - 11:00 AM

its like this:

$variablename= 'whatever';

echo $variablename;

# output: Whatever

echo '$variablename';

# output: $variablename

echo substr('$variablename',1);

# output: variablename <--- this is the one i need

what I need is to do is... Use the $variablename and print variablename just like the 3rd example but not using substr. Is there any other way?



#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 01 March 2006 - 12:26 PM

str_replace('$','','$variablename');
but thats identical to typing echo ('variablename');

What you actualy need it for? like if u show some example of what your using it in a may be able to help..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 slpwkr

slpwkr
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 06 March 2006 - 03:03 AM

Thanx, but I don't think what im trying to do is possible. Its something like this


function myfunc ($variable) {
global $array;

$array[$variable]=$variable;
}


$var1='my 1st string';
$var2='my 2nd string';

myfunct ($var1);
myfunct ($var2);

print_r($array);

#I want an output something like this: ('var1'=>'this is my 1st string', 'var2'=>'this is my 2nd string')



#6 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 06 March 2006 - 10:21 AM

Wouldn't this work then?

function myfunc ($variable) {
global $array;
$variable2=str_replace('$','','$variable);
$array["$variable2"]=$variable;
}


$var1='my 1st string';
$var2='my 2nd string';

myfunct ($var1);
myfunct ($var2);

print_r($array);

#I want an output something like this: ('var1'=>'this is my 1st string', 'var2'=>'this is my 2nd string')


Not sure if that's what your looking for..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 March 2006 - 03:52 PM

Try this code. It takes advantage of the superglobal array $GLOBALS. One thing you should be aware of is that if you have more than one variable with the same value, only the first one will be found.

<?php
function myfunc($var)
{
   $defk = 'no_var';
   foreach($GLOBALS as $k => $v) 
    if (!is_array($k) && $v == $var) return($k);
   return($defk);
}

        $test1 = "This is a test";
    $yat   = 'This is yet another test';
    $yat2 = 'This is yet another test';
    $new_array = array();
    $new_array[myfunc($test1)] = $test1;
    $new_array[myfunc($yat)] = $yat;
    $new_array[myfunc($yat2)] = $yat2;
    $tmp = array();
    foreach ($new_array as $k => $v)
        $tmp[] = "'" . $k . "' => '" . $v . "'";
    echo '<pre>(' . implode(', ',$tmp) . ')</pre>';
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users