php - str_replace unexpected behavior -
php - str_replace unexpected behavior -
i writing trivial templating scheme running dynamic queries on server.
i had next code in templating class:
$output = file_get_contents($this->file); foreach ($this->values $key => $value) { $tagtoreplace = "{$key}"; $output = str_replace($tagtoreplace, $value, $output); } i notice strings not beingness replaced expected (the '{}' characters still left in output) .
i changed 'offending' line to:
$tagtoreplace = '{'."$key".'}'; it worked expected. why alter necessary?. "{" in interpreted string have special significance in php?
yes. when using double quotes, "{$key}" , "$key" same. it's done can expand more complex variables, such "my name is: {$user['name']}".
you can utilize single quotes (as have), escape curly brackets -"\{$key\}"- or wrap variable twice: "{{$key}}".
read more here: http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
php str-replace
Comments
Post a Comment