FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » variable replacement in string
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: variable replacement in string [message #178053 is a reply to message #178006] Fri, 11 May 2012 14:06 Go to previous messageGo to previous message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma:
Member
In article <a11su1F3j9U2(at)mid(dot)uni-berlin(dot)de>,
"M. Strobel" <sorry_no_mail_here(at)nowhere(dot)dee> wrote:

> Am 10.05.2012 15:05, schrieb Goran:
>> 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',
>> ));
>
> yeah, create my own variable system. No easier way?
>
> /Str.

TESTED:

#!/usr/bin/php
<?

$t = "SOLVED";
$template = 'The problem is $t';

print fxx($template);

function fxx($string){
if (preg_match_all("/\\$([a-z_A-Z]+)/i", $string, $matches)) {
foreach($matches[1] as $m) {
global $$m;
}
$string = preg_replace(
"/\\$([a-zA-Z_]+)/sei",
"\${\\1}",
$string
);
}
return $string;
}
?>

Of course, having posted that, I'd still use caution with scopes and
globilzation of variables, since this function may be called inside
another function which hasn't globalized $t, so I would still
recommend to send along a variables of values:


#!/usr/bin/php
<?

$values["t"] = "SOLVED";
$template = 'The problem is $t';

print fxx($template, $values);

function fxx($string, $values){
$string = preg_replace(
"/\\$([a-zA-Z_]+)/sei",
"\$values['\\1']",
$string
);
return $string;
}
?>

And using single quotes when creating the template is important as
well, since we're using PHP-style variables. If you have your own
variable denotation, you don't have to think about that at all (see my
earlier parserxml() function)




--
Sandman[.net]
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: Windows binaries 64bit for PHP
Next Topic: Re: Windows binaries 64bit for PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 01 05:38:54 GMT 2024

Total time taken to generate the page: 0.04282 seconds