So I have some PHP code that looks like:
$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.
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?
I'm usually using this for my code, fetching the $text from some text/html files then make the $text_result as the output
If you use
%s
, I think that is the same placeholder that printf uses for a string. So you could do:Think that should work at least...
You can use
sprintf
, which works in a very similar way to C'sprintf
andsprintf
functions.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.
try dynamic variables:
then %s will be replaced with $s, the variable