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
Post a Comment