PHP basic questions for help!!!!!!
Here's the thing: I'm querying the MySQL database except for a record that contains variables such as {$aaa}, {$bbb},
Now I want to assign the two variables before the output, but the result of the output is the variable ....
For example:
$rs _sql =mysql_query ("Select content from table where id = ' 1 '");
$row _rs_sql = mysql_fetch_array ($rs _sql);
The contents of the Content field are: Hello {$aaa}, long time no see, I was thinking of you {$bbb}!///////
$AAA = "Andy Lau";
$BBB = "Lin Xin";
echo $row _rs_sql[' content ';
////I want the result is: Hello Andy Lau, long time no see, I want your Lin heart like!
But now the result of the output is still: Hello {$aaa}, long time no see, I was thinking of you {$bbb}!
I ask you how to solve this problem ah, very urgent!!!
Share to:
------Solution--------------------
echo preg_replace ('/\{\$ (\w+) \}/e ', ' $$1 ', $row _rs_sql[' content ');
------Solution--------------------
References:
Here's the thing: I'm querying the MySQL database except for a record that contains variables such as {$aaa}, {$bbb},
Now I want to assign the two variables before the output, but the result of the output is the variable ....
For example:
$rs _sql =mysql_query ("Select content from table where id = ' 1 '");
$row _rs_sql = mysql_fetch_array ($rs _sql);
The Content field is: Hello {$aaa}, long time no see, I was thinking of you {$bbb}! ///////
$AAA = "Andy Lau";
$BBB = "Lin Xin";
echo $row _rs_sql[' content ';
I want the result is: Hello Andy Lau, long time no see, I want your Lin heart like!
But now the result of the output is still: Hello {$aaa}, long time no see, I was thinking of you {$bbb}!
I ask you how to solve this problem ah, very urgent!!!
The contents of the Content field are: Hello {$aaa}, long time no see, I was thinking of you {$bbb}!The $aaa inside you is a string, not a variable, so replace it with $row_rs_sql = Str_replace (' {$aaa} ', $aaa), $row _rs_sql = Str_replace (' {$bbb} ', $bbb); echo $row _rs_sql;
------Solution--------------------
Do not use Str_replace (), refer to PHP eval () function http://php.net/manual/en/function.eval.php
$str = $row _rs_sql[' content '];
Eval ("\ $str = \" $str \ ";");
Echo $str;