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 #178019 is a reply to message #178015] Thu, 10 May 2012 18:12 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 5/10/2012 12:29 PM, M. Strobel wrote:
> Am 10.05.2012 15:14, schrieb Jerry Stuckle:
>> On 5/10/2012 8:44 AM, 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.
>>
>> There's also preg_replace(), but that's a bit of overkill for something so simple.
>>
>> str_replace() works fine, especially since both the search and replace parameters can
>> be arrays. But when using message templates, I don't use variable names - it ties
>> the template tightly to the code.
>>
>> Rather, I use templates like:
>>
>> $t = ' - solved - ';
>> $template = 'The problem is %PROB_STAT%';
>> echo str_replace('%PROB_STAT%', $t, $template);
>>
>> Or, if you had the possibility of more than one string to substitute:
>>
>> $search = array('$PROB_STAT%');
>> $replace = array($t);
>> echo str_replace('%PROB_STAT%', $t, $template);
>>
>> Your method requires the template know that $t is the value in the program, and
>> requires the program to ensure $t is defined properly in this scope. Neither is a
>> good idea.
>>
> I see the problem of the undefined $t.
>
> Think of my method as "do what double quotes do".
>
> It may well not exist, maybe I file a feature request.
>
> /Str.

No, you miss the point. You are also now closely coupling your template
to your code. Such coupling is not good - it severely limits possibilities.

For instance, let's say you want to use the same message another place
in your code - but you've already used $t. Do you rewrite the entire
routine? Or maybe you want to use your templates somewhere completely
different.

Tight coupling is bad because it creates more (and often unnecessary)
dependencies. Loose coupling limits dependencies, and fewer
dependencies means fewer errors crop into the code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)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: Thu Nov 28 14:02:12 GMT 2024

Total time taken to generate the page: 0.03937 seconds