PHP Replacing Character Inside String With Variable -
in few words, trying replace "?" value inside variable , doesn't work. please help.
$string = "? red ? blue"; $count = 1; update_query($string, array($v = 'violets', $r = 'roses')); function update_query($string, $values){ foreach ( $values $val ){ str_replace('?', $val, $string, $count); } echo $string; }
the output getting is: ? red ? blue
frustrated people not paying attention, compelled answer question properly.
str_replace
replace instances of search string. after violets
, there nothing left roses
replace.
sadly str_replace
not come limit parameter, preg_replace
does. can better still preg_replace_callback
, so:
function update_query($string, $values){ $result = preg_replace_callback('/\?/', function($_) use (&$values) { return array_shift($values); }, $string); echo $string; }
Comments
Post a Comment