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


Groups > comp.lang.php > #5964

Re: variable replacement in string

From Sandman <mr@sandman.net>
Newsgroups comp.lang.php
Subject Re: variable replacement in string
Date 2012-05-11 16:06 +0200
Message-ID <mr-17DD6B.16063711052012@News.Individual.NET> (permalink)
References <a11rgoFph0U1@mid.uni-berlin.de> <jogefj$6q5$1@news.metronet.hr> <a11su1F3j9U2@mid.uni-berlin.de>

Show all headers | View raw


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]

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web