如何用PHP替换字符串中的变量?

时间:2022-09-10 16:47:49

So I have some PHP code that looks like:

我有一些PHP代码

$message = 'Here is the result: %s';

I just used %s as an example. It's basically a placeholder for whatever will go there. Then I pass the string to a function and I want that function to replace the %s with the value.

我只是以%s为例。它基本上是一个占位符。然后我将字符串传递给一个函数,我希望这个函数用值替换%s。

What do I need to do to achieve this? Do I need to do some regex, and use preg_replace(), or something? or is there a simpler way to do it?

我需要做什么才能达到这个目标?我需要做一些regex,并使用preg_replace()或其他什么吗?或者有更简单的方法吗?

5 个解决方案

#1


21  

You can actually use the sprintf function which will return a formatted string and will put your variables on the place of the placeholders.
It also gives you great powers over how you want your string to be formatted and displayed.

实际上,您可以使用sprintf函数,该函数将返回格式化的字符串,并将变量放在占位符的位置。它还为您提供了关于如何格式化和显示字符串的强大功能。

$output = sprintf("Here is the result: %s for this date %s", $result, $date);

#2


3  

If you use %s, I think that is the same placeholder that printf uses for a string. So you could do:

如果您使用%s,我认为这与printf用于字符串的占位符相同。所以你可以做的:

$text = sprintf($message, "replacement text");

Think that should work at least...

认为这至少应该行得通……

#3


3  

$find = array(
     '#name#',
     '#date#'
);
$search = array(
     'someone\'s name',
     date("m-d-Y")
);
$text_result = str_replace($find, $search, $text);

I'm usually using this for my code, fetching the $text from some text/html files then make the $text_result as the output

我通常在代码中使用它,从一些文本/html文件中获取$文本,然后将$text_result作为输出

#4


2  

You can use sprintf, which works in a very similar way to C's printf and sprintf functions.

您可以使用sprintf,它的工作方式与C的printf和sprintf函数非常相似。

#5


-3  

try dynamic variables:

尝试动态变量:

$placeholder = 's';
str_replace("%".$placeholder,$$placeholder,$message);

then %s will be replaced with $s, the variable

然后用变量$s替换%s

#1


21  

You can actually use the sprintf function which will return a formatted string and will put your variables on the place of the placeholders.
It also gives you great powers over how you want your string to be formatted and displayed.

实际上,您可以使用sprintf函数,该函数将返回格式化的字符串,并将变量放在占位符的位置。它还为您提供了关于如何格式化和显示字符串的强大功能。

$output = sprintf("Here is the result: %s for this date %s", $result, $date);

#2


3  

If you use %s, I think that is the same placeholder that printf uses for a string. So you could do:

如果您使用%s,我认为这与printf用于字符串的占位符相同。所以你可以做的:

$text = sprintf($message, "replacement text");

Think that should work at least...

认为这至少应该行得通……

#3


3  

$find = array(
     '#name#',
     '#date#'
);
$search = array(
     'someone\'s name',
     date("m-d-Y")
);
$text_result = str_replace($find, $search, $text);

I'm usually using this for my code, fetching the $text from some text/html files then make the $text_result as the output

我通常在代码中使用它,从一些文本/html文件中获取$文本,然后将$text_result作为输出

#4


2  

You can use sprintf, which works in a very similar way to C's printf and sprintf functions.

您可以使用sprintf,它的工作方式与C的printf和sprintf函数非常相似。

#5


-3  

try dynamic variables:

尝试动态变量:

$placeholder = 's';
str_replace("%".$placeholder,$$placeholder,$message);

then %s will be replaced with $s, the variable

然后用变量$s替换%s