Replace text in variable

Here, for example, there is a variable with the text “Hello %s. Welcome to %s. And %s to you.”
How to replace %s with the words I need.
There is a list of words “user”, “site”, “have a nice day”.
And after replacing the s variable, there should already be this text:
“Hi user. Welcome to the site. And have a nice day.”

Thanks in advance.

Answer 1, authority 100%

Hello. It could be something like this:

$str = ' %s.    %s.  %s .';
$name = '';
$site = '';
$hi = ' ';
$str = sprintf($str, $name, $site, $hi);
echo $str;

This is just an example, and then you can implement it however you like.

Answer 2, authority 33%

You can do this:

$arr1 = array(" %s", "   %s", " %s ");
$arr2 = array("", "", " ");
for($i = 0;$i<=count($arr1)-1;$i++)
$text[$i] = str_replace("%s",$arr2[$i], $arr1[$i]);

Leave a reply

Please enter your comment!
Please enter your name here