Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5913 > unrolled thread

variable replacement in string

Started by"M. Strobel" <sorry_no_mail_here@nowhere.dee>
First post2012-05-10 14:44 +0200
Last post2012-05-11 10:31 +0200
Articles 20 on this page of 27 — 8 participants

Back to article view | Back to comp.lang.php


Contents

  variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 14:44 +0200
    Re: variable replacement in string The Natural Philosopher <tnp@invalid.invalid> - 2012-05-10 14:00 +0100
      Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 15:07 +0200
    Re: variable replacement in string Goran <goran@nospam.com> - 2012-05-10 15:05 +0200
      Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 15:08 +0200
        Re: variable replacement in string The Natural Philosopher <tnp@invalid.invalid> - 2012-05-10 14:11 +0100
          Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 18:21 +0200
            Re: variable replacement in string The Natural Philosopher <tnp@invalid.invalid> - 2012-05-10 21:50 +0100
              Re: variable replacement in string Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-10 17:06 -0400
                Re: variable replacement in string Scott Johnson <noonehome@chalupasworld.com> - 2012-05-10 15:31 -0700
                  Re: variable replacement in string The Natural Philosopher <tnp@invalid.invalid> - 2012-05-11 01:08 +0100
        Re: variable replacement in string Goran <goran@nospam.com> - 2012-05-10 15:13 +0200
          Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 18:23 +0200
        Re: variable replacement in string Sandman <mr@sandman.net> - 2012-05-11 16:06 +0200
    Re: variable replacement in string Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-10 09:14 -0400
      Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 18:29 +0200
        Re: variable replacement in string Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-10 14:12 -0400
    Re: variable replacement in string Jeff North <jnorthau@yahoo.com.au> - 2012-05-11 00:04 +1000
      Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 18:32 +0200
        Re: variable replacement in string Jeff North <jnorthau@yahoo.com.au> - 2012-05-11 12:30 +1000
          Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-11 09:32 +0200
    Re: variable replacement in string Sandman <mr@sandman.net> - 2012-05-10 16:10 +0200
      Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 18:37 +0200
        Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-10 18:38 +0200
        Re: variable replacement in string Sandman <mr@sandman.net> - 2012-05-11 15:48 +0200
    Re: variable replacement in string "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-05-11 09:58 +0200
      Re: variable replacement in string "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-11 10:31 +0200

Page 1 of 2  [1] 2  Next page →


#5913 — variable replacement in string

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-10 14:44 +0200
Subjectvariable replacement in string
Message-ID<a11rgoFph0U1@mid.uni-berlin.de>
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.

[toc] | [next] | [standalone]


#5914

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2012-05-10 14:00 +0100
Message-ID<joge4r$mcm$2@news.albasani.net>
In reply to#5913
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.

what is wrong with printf("the problem is %s \n",$t);


or

echo ("the problem is".$t);

???


-- 
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.

[toc] | [prev] | [next] | [standalone]


#5916

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-10 15:07 +0200
Message-ID<a11ss6F3j9U1@mid.uni-berlin.de>
In reply to#5914
Am 10.05.2012 15:00, schrieb The Natural Philosopher:
> 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.
> 
> what is wrong with printf("the problem is %s \n",$t);

relating %s to $t is extra work.

> 
> 
> or
> 
> echo ("the problem is".$t);
> 

or

echo 'the problem is ', $t;

be sure I can do it in code. In Tcl, interactive session:

strobel@s114-intel:~> tclsh
% set t " - solved - "
 - solved -
% set msg {the problem is $t}
the problem is $t
% puts $msg
the problem is $t
% puts [subst $msg]
the problem is  - solved -
%

/Str.




[toc] | [prev] | [next] | [standalone]


#5915

FromGoran <goran@nospam.com>
Date2012-05-10 15:05 +0200
Message-ID<jogefj$6q5$1@news.metronet.hr>
In reply to#5913
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',
    ));

[toc] | [prev] | [next] | [standalone]


#5917

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-10 15:08 +0200
Message-ID<a11su1F3j9U2@mid.uni-berlin.de>
In reply to#5915
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.

[toc] | [prev] | [next] | [standalone]


#5918

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2012-05-10 14:11 +0100
Message-ID<jogep7$nov$1@news.albasani.net>
In reply to#5917
M. Strobel 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.
> 
if you want to program in tcl., i suggest you use tcl.

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

should be
$t = ' - solved - ';
$msg = 'The problem is'.$t;
echo($msg);


-- 
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.

[toc] | [prev] | [next] | [standalone]


#5924

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-10 18:21 +0200
Message-ID<a12891Fn6tU1@mid.uni-berlin.de>
In reply to#5918
Am 10.05.2012 15:11, schrieb The Natural Philosopher:

> if you want to program in tcl., i suggest you use tcl.
> 
> your example
> $t = ' - solved - ';
> $msg = 'The problem is $t';
> 
> should be
> $t = ' - solved - ';
> $msg = 'The problem is'.$t;
> echo($msg);

You cannot miss something you do not know it exists.

/Str.

[toc] | [prev] | [next] | [standalone]


#5938

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2012-05-10 21:50 +0100
Message-ID<joh9md$ffq$10@news.albasani.net>
In reply to#5924
M. Strobel wrote:
> Am 10.05.2012 15:11, schrieb The Natural Philosopher:
> 
>> if you want to program in tcl., i suggest you use tcl.
>>
>> your example
>> $t = ' - solved - ';
>> $msg = 'The problem is $t';
>>
>> should be
>> $t = ' - solved - ';
>> $msg = 'The problem is'.$t;
>> echo($msg);
> 
> You cannot miss something you do not know it exists.
> 

Of course you can. Don't be silly.

I've missed loads of things that I didnt know about.

Like the flight to Chicago. I've missed every single flight to chicago, 
ever. Most before I even knew there was one. :-)



> /Str.
> 


-- 
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.

[toc] | [prev] | [next] | [standalone]


#5940

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-05-10 17:06 -0400
Message-ID<johal0$c90$2@dont-email.me>
In reply to#5938
On 5/10/2012 4:50 PM, The Natural Philosopher wrote:
> M. Strobel wrote:
>> Am 10.05.2012 15:11, schrieb The Natural Philosopher:
>>
>>> if you want to program in tcl., i suggest you use tcl.
>>>
>>> your example
>>> $t = ' - solved - ';
>>> $msg = 'The problem is $t';
>>>
>>> should be
>>> $t = ' - solved - ';
>>> $msg = 'The problem is'.$t;
>>> echo($msg);
>>
>> You cannot miss something you do not know it exists.
>>
>
> Of course you can. Don't be silly.
>
> I've missed loads of things that I didnt know about.
>
> Like the flight to Chicago. I've missed every single flight to chicago,
> ever. Most before I even knew there was one. :-)
>
>
>
>> /Str.
>>
>
>

Which makes the people of Chicago VERY HAPPY!

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#5943

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-05-10 15:31 -0700
Message-ID<johfjo$8jc$1@dont-email.me>
In reply to#5940
On 5/10/2012 2:06 PM, Jerry Stuckle wrote:
> On 5/10/2012 4:50 PM, The Natural Philosopher wrote:
>> M. Strobel wrote:
>>> Am 10.05.2012 15:11, schrieb The Natural Philosopher:
>>>
>>>> if you want to program in tcl., i suggest you use tcl.
>>>>
>>>> your example
>>>> $t = ' - solved - ';
>>>> $msg = 'The problem is $t';
>>>>
>>>> should be
>>>> $t = ' - solved - ';
>>>> $msg = 'The problem is'.$t;
>>>> echo($msg);
>>>
>>> You cannot miss something you do not know it exists.
>>>
>>
>> Of course you can. Don't be silly.
>>
>> I've missed loads of things that I didnt know about.
>>
>> Like the flight to Chicago. I've missed every single flight to chicago,
>> ever. Most before I even knew there was one. :-)
>>
>>
>>
>>> /Str.
>>>
>>
>>
>
> Which makes the people of Chicago VERY HAPPY!
>

Time to get the microwave popcorn ready!

[toc] | [prev] | [next] | [standalone]


#5946

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2012-05-11 01:08 +0100
Message-ID<johlae$5d2$4@news.albasani.net>
In reply to#5943
Scott Johnson wrote:
> On 5/10/2012 2:06 PM, Jerry Stuckle wrote:
>> On 5/10/2012 4:50 PM, The Natural Philosopher wrote:
>>> M. Strobel wrote:
>>>> Am 10.05.2012 15:11, schrieb The Natural Philosopher:
>>>>
>>>>> if you want to program in tcl., i suggest you use tcl.
>>>>>
>>>>> your example
>>>>> $t = ' - solved - ';
>>>>> $msg = 'The problem is $t';
>>>>>
>>>>> should be
>>>>> $t = ' - solved - ';
>>>>> $msg = 'The problem is'.$t;
>>>>> echo($msg);
>>>>
>>>> You cannot miss something you do not know it exists.
>>>>
>>>
>>> Of course you can. Don't be silly.
>>>
>>> I've missed loads of things that I didnt know about.
>>>
>>> Like the flight to Chicago. I've missed every single flight to chicago,
>>> ever. Most before I even knew there was one. :-)
>>>
>>>
>>>
>>>> /Str.
>>>>
>>>
>>>
>>
>> Which makes the people of Chicago VERY HAPPY!
>>
> 
> Time to get the microwave popcorn ready!

Hardly,. His lord High Stuckupness has been in my killfile a LONG time, 
as he never says anything worth reading - as evinced by this 
particularly puerile offering.


-- 
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.

[toc] | [prev] | [next] | [standalone]


#5919

FromGoran <goran@nospam.com>
Date2012-05-10 15:13 +0200
Message-ID<jogetk$6q5$2@news.metronet.hr>
In reply to#5917
On 10.5.2012 15:08, M. Strobel wrote:
> yeah, create my own variable system. No easier way?

Yes of course, there are some alternative techniques like http://bit.ly/NVYN

[toc] | [prev] | [next] | [standalone]


#5925

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-10 18:23 +0200
Message-ID<a128crFn6tU2@mid.uni-berlin.de>
In reply to#5919
Am 10.05.2012 15:13, schrieb Goran:
> On 10.5.2012 15:08, M. Strobel wrote:
>> yeah, create my own variable system. No easier way?
> 
> Yes of course, there are some alternative techniques like http://bit.ly/NVYN

Haha. I read Stanislav Lem in the seventies, far more imagination...

/Str.

[toc] | [prev] | [next] | [standalone]


#5964

FromSandman <mr@sandman.net>
Date2012-05-11 16:06 +0200
Message-ID<mr-17DD6B.16063711052012@News.Individual.NET>
In reply to#5917
In article <a11su1F3j9U2@mid.uni-berlin.de>,
 "M. Strobel" <sorry_no_mail_here@nowhere.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]

[toc] | [prev] | [next] | [standalone]


#5920

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-05-10 09:14 -0400
Message-ID<jogf05$m8e$1@dont-email.me>
In reply to#5913
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.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#5926

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-10 18:29 +0200
Message-ID<a128neFrsoU1@mid.uni-berlin.de>
In reply to#5920
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.

[toc] | [prev] | [next] | [standalone]


#5930

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-05-10 14:12 -0400
Message-ID<joh0f8$9qr$1@dont-email.me>
In reply to#5926
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#5921

FromJeff North <jnorthau@yahoo.com.au>
Date2012-05-11 00:04 +1000
Message-ID<cminq7lal2bu5eefmtef6kor2ko96f2ano@4ax.com>
In reply to#5913
On Thu, 10 May 2012 14:44:08 +0200, in comp.lang.php "M. Strobel"
<sorry_no_mail_here@nowhere.dee>
<a11rgoFph0U1@mid.uni-berlin.de> 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.

How about:
$msg = 'The problem is {$t}';

[toc] | [prev] | [next] | [standalone]


#5927

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-10 18:32 +0200
Message-ID<a128trFt9iU1@mid.uni-berlin.de>
In reply to#5921
Am 10.05.2012 16:04, schrieb Jeff North:
> On Thu, 10 May 2012 14:44:08 +0200, in comp.lang.php "M. Strobel"
> <sorry_no_mail_here@nowhere.dee>
> <a11rgoFph0U1@mid.uni-berlin.de> 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.
> 
> How about:
> $msg = 'The problem is {$t}';

And the next step? How do you print that the problem is solved?

But maybe you could do a regex replace with this...

/Str.

[toc] | [prev] | [next] | [standalone]


#5949

FromJeff North <jnorthau@yahoo.com.au>
Date2012-05-11 12:30 +1000
Message-ID<sauoq793q825coiasa8tje12buil6k50td@4ax.com>
In reply to#5927
On Thu, 10 May 2012 18:32:58 +0200, in comp.lang.php "M. Strobel"
<sorry_no_mail_here@nowhere.dee>
<a128trFt9iU1@mid.uni-berlin.de> wrote:

>| Am 10.05.2012 16:04, schrieb Jeff North:
>| > On Thu, 10 May 2012 14:44:08 +0200, in comp.lang.php "M. Strobel"
>| > <sorry_no_mail_here@nowhere.dee>
>| > <a11rgoFph0U1@mid.uni-berlin.de> 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.
>| > 
>| > How about:
>| > $msg = 'The problem is {$t}';
>| 
>| And the next step? How do you print that the problem is solved?
>| 
>| But maybe you could do a regex replace with this...
>| 
>| /Str.

echo $msg;

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.php


csiph-web