Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2006, 09:17 AM   PM User | #1
The Reverend
New Coder

 
Join Date: Mar 2006
Location: I'm lost, livin inside myself
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
The Reverend is an unknown quantity at this point
problem with nested variables

Okay, here's the problem. I have a string I retrieve from the database that contains several PHP variables, I want to evaluate it and have the variables filled in, I'm just not sure how to do this.

This is the string I'm retrieving for example:

Code:
<b>$test</b>
before I retrieve it I declare the variable $test, but when I print it out it comes out as this "$test", and doesn't evaluate. How do I do this? I tried using the eval() function and it still doesn't change the output. This is a simplified example of what I'm actually doing to make it easier to solve, if anyone can help I'd appreciate it.
The Reverend is offline   Reply With Quote
Old 11-30-2006, 01:03 PM   PM User | #2
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
PHP Code:
$test "bob";
$string '<b>$test</b>';

eval(
"\$result = ".$string);
echo 
$result
Without seeing your code this is the best I can do - I havent tried it but it should work
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 11-30-2006, 02:17 PM   PM User | #3
Tyree
Regular Coder

 
Tyree's Avatar
 
Join Date: Sep 2003
Posts: 254
Thanks: 0
Thanked 0 Times in 0 Posts
Tyree is on a distinguished road
This probably isn't as simple as I'm seeing it...but are you wrapping your php in "<?php ?>" ? The example you show is not going to do anything but what you said it does.

Here's two examples of ways to display the value of $test:
PHP Code:
<?php
$test 
"blah";
?>

<b><?php print $test?></b>
OR

PHP Code:
<?php
$test 
"blah";

print 
"<br>".$test."</br>";
?>
You can't mix html and php without telling the browser what is what.

Hope that helps!
Tyree is offline   Reply With Quote
Old 11-30-2006, 06:48 PM   PM User | #4
The Reverend
New Coder

 
Join Date: Mar 2006
Location: I'm lost, livin inside myself
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
The Reverend is an unknown quantity at this point
It's a little more complicated than that, I tried using eval and it doesn't work. Here's a little more complicated version to give you a better idea.

PHP Code:
<?php
$test 
'test';

$result mysql_query("SELECT `field` FROM `table` WHERE id='1' LIMIT 1");
while(
$row mysql_fetch_assoc($result))
{
$string $row['field'//this value retrived is "<b>$test</b>"
}
echo 
$string;

//output is "<b>$test</b>", not "<b>test</b>" like I want

?>
Again, this is a simplified example of what I'm doing.
__________________
$guiness &= new sixpack();
$guiness->chug();
The Reverend is offline   Reply With Quote
Old 11-30-2006, 07:32 PM   PM User | #5
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
eval does it, you've just got to make sure the evaled string ends up with double quotes around the target string.

PHP Code:
$test 'asdf';
$othervar 'qwerty';
$string '<b>$test</b><i>$othervar</i>';

eval(
'$replaced_string = "' $string '";');
echo 
$replaced_string# <b>asdf</b><i>qwerty</i> 
A better approach I think would be to use a table:

PHP Code:
function make_replacements($string$replacements)
{
        foreach (
$replacements as $tag=>$value)
        {
                
$string str_replace($tag$value$string);
        }
        return 
$string;
}
$string '<b>$test</b><i>$othervar</i>';
static 
$replacements = array( 
        
'$test'     => 'asdf'
        
'$othervar' => 'qwerty' 
);
echo 
make_replacements($string$replacements); # <b>asdf</b><i>qwerty</i> 

Last edited by ralph l mayo; 11-30-2006 at 07:50 PM..
ralph l mayo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:26 PM.


Advertisement
Log in to turn off these ads.