PHP-用一个像字符串一样的变量替换一个字符串 [英] PHP - replace a string with a variable named like the string

查看:76
本文介绍了PHP-用一个像字符串一样的变量替换一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以字符串是这样的:

"bla bla bla {VARIABLE} bla bla"

当我在函数中的某处使用此字符串时,我想用 $ variable (或其他任何用{}字符括起来的大写字符串)替换 {VARIABLE} . $ variable(和其他任何变量)将在该函数内定义

when I use this string somewhere in a function I want to replace {VARIABLE} with $variable (or any other uppercase strings with wrapped within {} charcters). $variable (and any other variables) will be defined inside that function

我可以这样做吗?

推荐答案

$TEST = 'one';
$THING = 'two';
$str = "this is {TEST} a {THING} to test";

$result = preg_replace('/\{([A-Z]+)\}/e', "$$1", $str);

这篇关于PHP-用一个像字符串一样的变量替换一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆