Re: variable replacement in string



On 10.5.2012 14:44, M. Strobel wrote:
Hi,

I am still searching a function in PHP to execute variable replacement in strings.
Other languages do have this, but for PHP I can only find sprintf() and string replace.

I have

$t = ' - solved - ';
$msg = 'The problem is $t';

I want now:

echo fxx($msg);

print out "The problem is - solved - ".

Please don't tell me about $msg = "The problem is $t"; just think of $msg like a
template read from a file.

/Str.

strtr() ?

You could do something like this:

$template = 'Your name is %given_name% %family_name%';

echo strtr($template, array(
'%given_name%' => 'John',
'%family_name%' => 'Doe',
));
.



Relevant Pages

  • Re: variable replacement in string
    ... I am still searching a function in PHP to execute variable replacement in strings. ... echo; ... % set msg ...
    (comp.lang.php)
  • Re: Problem with quotes in javascript output
    ... should be passing either strings for all the attributes, ... echo "file: 'http://www.youtube.com/watch?v='"; ... it is even easier to json_encodea PHP array as long as the leaf ... evangelism: XHTML 1.0 Strict ...
    (comp.lang.php)
  • Re: Pascal Server Page
    ... this is a Delphi forum, ... assume everybody here prefers Delphi over other languages, ... but PHP is better suited for web ... PHP supports varaiable interpolation in strings which makes ...
    (borland.public.delphi.non-technical)
  • Re: variable replacement in string
    ... I am still searching a function in PHP to execute variable replacement in strings. ... but for PHP I can only find sprintfand string replace. ... echo strtr($template, array( ... if you want to program in tcl., ...
    (comp.lang.php)
  • Re: variable replacement in string
    ... I am still searching a function in PHP to execute variable replacement in strings. ... but for PHP I can only find sprintfand string replace. ... echo strtr($template, array( ...
    (comp.lang.php)