Php 如何用实际值替换占位符?

Php 如何用实际值替换占位符?,php,Php,我需要一个函数,用正确的变量替换“{}”中的每个变量名。 大概是这样的: $data["name"] = "Johnny"; $data["age"] = "20"; $string = "Hello my name is {name} and I'm {age} years old."; $output = replace($string, $data); echo $output; //outputs: Hello my name is Johnny and I'm 20 years

我需要一个函数,用正确的变量替换“{}”中的每个变量名。 大概是这样的:

$data["name"] = "Johnny";
$data["age"] = "20";

$string = "Hello my name is {name} and I'm {age} years old.";

$output = replace($string, $data);
echo $output;

//outputs: Hello my name is Johnny and I'm 20 years old.

我知道有用于此的框架/引擎,但我不想只为此安装一堆文件。

您可能需要查看函数。

您可能需要查看函数。

您可以使用
preg\u replace
/e
修饰符轻松完成此操作:

$data["name"] = "Johnny";
$data["age"] = "20";

$string = "Hello my name is {name} and I'm {age} years old.";

echo preg_replace('/{(\w+)}/e', '$data["\\1"]', $string);


您可能希望自定义与替换字符串相匹配的模式(这里是
{\w+}
:在花括号之间有一个或多个字母数字字符或下划线)。将其放入函数中非常简单。

您可以使用
preg\u replace
/e
修饰符轻松完成此操作:

$data["name"] = "Johnny";
$data["age"] = "20";

$string = "Hello my name is {name} and I'm {age} years old.";

echo preg_replace('/{(\w+)}/e', '$data["\\1"]', $string);

您可能希望自定义与替换字符串相匹配的模式(这里是
{\w+}
:在花括号之间有一个或多个字母数字字符或下划线)。将其放入函数中非常简单。

给你:

$data["name"] = "Johnny";
$data["age"] = "20";

$string = "Hello my name is {name} and I'm {age} years old.";

foreach ($data as $key => $value) {
$string = str_replace("{".$key."}", $value, $string);
}

echo $string;
给你:

$data["name"] = "Johnny";
$data["age"] = "20";

$string = "Hello my name is {name} and I'm {age} years old.";

foreach ($data as $key => $value) {
$string = str_replace("{".$key."}", $value, $string);
}

echo $string;
我会做你想做的。如果它不适合您,请尝试使用正则表达式执行类似于循环的操作,如下所示

for ($data as $key=>$value){
    $string = preg_replace("\{$key\}", $value, $string);
}
未经测试,您可能需要查阅文档

我会做你想做的。如果它不适合您,请尝试使用正则表达式执行类似于循环的操作,如下所示

for ($data as $key=>$value){
    $string = preg_replace("\{$key\}", $value, $string);
}
未经测试,您可能需要查阅文档。

您可以尝试它的语法稍有不同

$string = 'hello my name is %s and I am %d years old';

$params = array('John', 29);

var_dump(vsprintf($string, $params));
//string(43) "hello my name is John and I am 29 years old" 
您可以尝试一下,它的语法略有不同

$string = 'hello my name is %s and I am %d years old';

$params = array('John', 29);

var_dump(vsprintf($string, $params));
//string(43) "hello my name is John and I am 29 years old" 

我一直是strtr的粉丝

$ php -r 'echo strtr("Hi @name. The weather is @weather.", ["@name" => "Nick", "@weather" => "Sunny"]);'
Hi Nick. The weather is Sunny.
另一个优点是可以定义不同的占位符前缀类型。Drupal就是这样做的
@
表示要转义为安全输出到网页的字符串(以避免注入攻击)。该命令在您的参数(例如
@name
@weather
)上循环,如果第一个字符是
@
,则它使用
检查值


也在这里回答:

我一直是
strtr
的粉丝

$ php -r 'echo strtr("Hi @name. The weather is @weather.", ["@name" => "Nick", "@weather" => "Sunny"]);'
Hi Nick. The weather is Sunny.
另一个优点是可以定义不同的占位符前缀类型。Drupal就是这样做的
@
表示要转义为安全输出到网页的字符串(以避免注入攻击)。该命令在您的参数(例如
@name
@weather
)上循环,如果第一个字符是
@
,则它使用
检查值


也在这里回答:

很好的解决方案,+1。虽然我会使用
[^\}]+
而不是
\w
很好的解决方案,+1。虽然我会用
[^\}]+
而不是
\w
但是有什么特别的理由用
preg\u replace
而不是
str\u replace
在这里吗?有什么特别的理由用
preg\u replace
而不是
str\u replace
在这里吗?