如何在 PHP 中使用可变变量?

发布于 2024-08-03 18:15:37 字数 303 浏览 6 评论 0原文

这就是我想要做的:

<?php
    $my_str = "My String";
    $str = "%my_str";

    $str = str_replace("%", "$", $str);

    echo $str;
?>

上面的代码将“$my_str”打印到屏幕上。但我希望它打印“我的字符串”,就像变量 $my_str 的实际值一样

有人知道如何做到这一点吗?

我想要这个的原因是因为我正在编写自己的非常基本的解析语言,所以我需要它发挥作用才能继续。

Here is what I am trying to do:

<?php
    $my_str = "My String";
    $str = "%my_str";

    $str = str_replace("%", "$", $str);

    echo $str;
?>

The above code prints '$my_str' to the screen. But I want it to print 'My String', as in the actual value of the variable $my_str

Anyone know how to do this?

The reason I want this is because I'm in the process of writing my own, very basic, parsing language so I kinda need this to be functional before I can continue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

避讳 2024-08-10 18:15:37
$my_str = 'My String';
$str    = 'my_str';

echo $str;

这种构造称为变量变量

$my_str = 'My String';
$str    = 'my_str';

echo $str;

This construction is called a variable variable.

强者自强 2024-08-10 18:15:37

评估是没有必要的。只需去掉 % 并使用 $$str

<?php
    $my_str = "My String";
    $str = "%my_str";

    $str = str_replace("%", "", $str);

    echo $str;
?>

eval is not necessary. Just get rid of the % and use $$str

<?php
    $my_str = "My String";
    $str = "%my_str";

    $str = str_replace("%", "", $str);

    echo $str;
?>
泪痕残 2024-08-10 18:15:37

您可以使用 preg_replacee 修饰符搜索并替换 %var 模式,这使得替换被评估:

<?php
    $my_str = "My String";
    $str = "Foo %my_str bar";

    $str = preg_replace("/%([A-Za-z_]+)/e", "$\\1", $str);

    echo $str;
?>

这里 preg_replace< /code> 会找到 %my_str\1 包含 my_str"$\\1" (反斜杠需要转义)成为 $my_str 的值。

但是,将替换字符串存储在关联数组中可能会更清晰:

<?php
    $replacements = array(
        "my_str" => "My String",
    );
    $str = "Foo %my_str bar";

    $str = preg_replace("/%([A-Za-z_]+)/e", '$replacements["\\1"]', $str);

    echo $str;
?>

You could search and replace the %var patterns using preg_replace and the e modifier, which makes the replacement being evaluated:

<?php
    $my_str = "My String";
    $str = "Foo %my_str bar";

    $str = preg_replace("/%([A-Za-z_]+)/e", "$\\1", $str);

    echo $str;
?>

Here preg_replace will find %my_str, \1 contains my_str and "$\\1" (the backslash needs to be escaped) becomes the value of $my_str.

However, it would maybe be cleaner to store your replacement strings in an associative array:

<?php
    $replacements = array(
        "my_str" => "My String",
    );
    $str = "Foo %my_str bar";

    $str = preg_replace("/%([A-Za-z_]+)/e", '$replacements["\\1"]', $str);

    echo $str;
?>
迷荒 2024-08-10 18:15:37

尝试:

echo $str;

Try:

echo $str;
完美的未来在梦里 2024-08-10 18:15:37

PHP 自动解析和扩展 double 内的变量- 带引号的字符串

<?php
    $my_str = "My String";
    $str = "{$my_str}";

    echo $str;
?>

输出:

My String

PHP automatically parses and expands variables within double-quoted strings:

<?php
    $my_str = "My String";
    $str = "{$my_str}";

    echo $str;
?>

Outputs:

My String
梦里人 2024-08-10 18:15:37

如何使用带有 e 修饰符的正则表达式

$str = preg_replace("/%([a-zA-Z0-9_]+)/e", '$\1', $str);

(这是未经测试的,但应该可以工作)

How about using a regex with the e modifier

$str = preg_replace("/%([a-zA-Z0-9_]+)/e", '$\1', $str);

(This is untested, but should work)

戏舞 2024-08-10 18:15:37

您看到的行为并不奇怪 - 变量扩展(“插值”)仅在字符串文字上执行,而不是在变量上执行。如果不是,那么这将是一个很大的安全漏洞(您的代码使用的任何带有 $ 的字符串实例都会突然泄露变量)。

可以尝试使用 eval 来修复这个问题,就像这样,

eval ("\$str = \"" . str_replace("%", "\$", $str) . "\"");

但这非常危险 - 如果你的字符串来自用户,那么可以使 $str 类似

"; system("rm -rf /"); $x = "

,突然间,你就有麻烦了。我相信,最好的解决方案是使用您最喜欢的方法(stripos 和 substring 或其他方法)解析变量,然后手动替换每个变量。

The behaviour you see is not surprising - expansion of variables ("interpolation") is performed on only string literals, rather than variables. If it wasn't, then it would be a large security hole (any instance of a string with $ in it that your code used would suddenly be revealing variables).

You can try and fix this using eval, as in

eval ("\$str = \"" . str_replace("%", "\$", $str) . "\"");

but this pretty dangerous - if your string is from the user, then can make $str something like

"; system("rm -rf /"); $x = "

and suddenly, you're in trouble. The best solution, I believe, will be to parse out variables using your favourite methods (stripos and substring, or something more), then replace each one by hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文