PHP String Replacement -
i wish have 1 string contains instance (mystring):
file config.php
$mystring = "hello name $name , got $school";
file index.php
include('config.php'); $name = $_get['name']; $school = $_get['school']; echo $mystring;
would work ? or there better ways
$string = 'hello, name %s , go %s'; printf($string, $_get['name'], $_get['school']);
or
$string = 'hello, name :name , go :school'; echo str_replace(array(':name', ':school'), array($_get['name'], $_get['school']), $string);
you can automate last 1 like:
function value_replace($values, $string) { return str_replace(array_map(function ($v) { return ":$v"; }, array_keys($values)), $values, $string); } $string = 'hello, name :name , go :school'; echo values_replace($_get, $string);
Comments
Post a Comment