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

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -