Php 从MySQL结果解析变量
我的mysql列中有一个嵌套变量,现在打印出来了,我希望它也能在我的脚本中被解析,但不知道怎么做,我甚至尝试过使用doublePhp 从MySQL结果解析变量,php,mysql,Php,Mysql,我的mysql列中有一个嵌套变量,现在打印出来了,我希望它也能在我的脚本中被解析,但不知道怎么做,我甚至尝试过使用double$$id,但仍然不起作用 以下是我所拥有的: 从mysql中提取,$message now has=您的订单$$id尚未准备就绪。 $id="234"; echo $message; 印刷品: Your order $$id is not ready yet. 我真的想让它显示: Your order 234 is not ready yet. 非常感谢您对我的任何
$$id
,但仍然不起作用
以下是我所拥有的:
从mysql中提取,$message now has=您的订单$$id尚未准备就绪。
$id="234";
echo $message;
印刷品:
Your order $$id is not ready yet.
我真的想让它显示:
Your order 234 is not ready yet.
非常感谢您对我的任何帮助 由于
$message
来自数据库,因此它应该如下所示,在这种情况下,您可以执行以下操作
$message = 'Your order $$id is not ready yet';
$id = "234";
$message = str_replace('$$id', $id, $message);
print $message;
为了完整性-如果字符串和替换值在一个数组中-您可以使其更平滑:
$strings = array(
'Your order $$id is not ready yet',
'Thank you $$name for your order',
);
$replacements = array(
'id' => "234",
'name' => "thevoipman",
);
foreach($strings as &$message)
{
foreach($replacements as $placeholder => $value)
{
$message = str_replace('$$' . $placeholder, $value, $message);
}
}
print_r($strings);
@habibulhaq$message打印
您的订单$$id还没有准备好。
他的意思是,消息是如何定义的?我讨厌说eval
不需要eval,如果$message
中有占位符,它只需要替换Hanks,但我希望有一种更快的方法不使用stru replace。。。。。。我做了一个mysql选择来获取$message@thevoipman你需要更换它,这是一个额外的行-我更新,以匹配您的消息format@thevoipman我已经添加了一个示例,说明如何以较少重复的方式进行操作。@thevoipman没有真正的解决方法。即使你没有显式地用PHP编写它,它也会在幕后发生。@thevoipman然后你必须问自己还有什么工作要做-重构一切使之更容易,或者只是添加10xstru\u replace