Php dynamically replace placeholder variables in string -


i want dynamically replace placeholder variables in string.

str_replace("\$", $data["whatever follows \$], $variable); 

\$ signifies placeholder variable, \$id example.

the data want replace in array. $\id should replaced $data['id'].

for example, if have string says "the id \$id , name \$name". want replace both \$id , \$name relevant data in $data object. $\id $data['id'] , on.

this needs dynamic. don't want hard code replace \$id $data['id']. key used data in $data should equal follows \$.

i'm having trouble figuring out not how dynamically have talked about, every \$ in string.

use printf() or sprintf().

if mark placeholder number, can repeat number of times in string.

sprintf("this test string. here's placeholder: %1$s, , another: %2$s, first 1 again: %1$s", $var1, $var2); 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -