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


Groups > comp.lang.php > #5922

Re: variable replacement in string

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Sandman <mr@sandman.net>
Newsgroups comp.lang.php
Subject Re: variable replacement in string
Date Thu, 10 May 2012 16:10:14 +0200
Lines 82
Message-ID <mr-739F0E.16101410052012@News.Individual.NET> (permalink)
References <a11rgoFph0U1@mid.uni-berlin.de>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace individual.net ESx7JpwcgWLm3uXvbjMFNQC3mrbMtzXDSZI4LaTezdhS5b/DLAsQjzmzHcBygFK0A=
X-Orig-Path mr
Cancel-Lock sha1:fFYhK5F2LEbPKzeCmxqDGRB0Tps=
User-Agent MT-NewsWatcher/3.5.2 (Intel Mac OS X)
X-Face $@,Vfa$,)%=Qa7L]y)&oZj_\EiHc}}A<Y3TvbI2&|e"bE9zc[o<ThMgB4%*L$b1YsNl!/ <lHO$>f0Bei"4a_%)"c6TQ+P/:53>;PNGuWUmkqyeN-qM65foJ[;T_(k;>]&G\T4Lhm:2 ujye2_,iUJFE;NZn>y;.|-hl7g~bIOF1qG\o<?]4mXkW*mT3]{Bn&VwP7(M0uYnGA!V!? {"y?BkBDW6e-.=I5
X-Killfiled yttrx, gallopinginsanity.com, Mark Kent, Maverick, Nasht.n, NRen2, MuahMan, weedhopper, PC Guy, Brian, nospam@nospam.com, Oxford, Jim Lee Jr., Mocassin Joe, zara, Chance Furlong, Robert Whelan, jt2002a@hotmail.com
Xref csiph.com comp.lang.php:5922

Show key headers only | View raw


In article <a11rgoFph0U1@mid.uni-berlin.de>,
 "M. Strobel" <sorry_no_mail_here@nowhere.dee> 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.

Before coding PHP I used Roxen and their "RXML" language, which was 
pretyt much just fancy HTML (RXML stood for Roxen Markup Language) and 
they had variable templating in strings. When moving to PHP I missed 
this.

So I have this templet string for headline formatting in blog posts 
(for instance):

    $template = "<h2>#headline#</h2>
     #date#, <span class='subline'>#category#</span>";

And then I have an array with the blog post:

    $article = array(
        "headline" => "Hello world!",
        "date" => "2012-04-23",
        "category" => "Fun stuff"
    );

And I have this function I call "parserxml()" which is "Parse rxml" 
not "parser xml" :)

    function parserxml($string, $var = false) {
        # RXML 1.0
        $string = preg_replace("/#([a-z]+)#/se", "\$var['\\1']", 
$string);
        # RXML 2.0
        if (preg_match_all("/&([a-z_A-Z]+)\./i", $string, $matches)) {
            foreach($matches[1] as $m) {
                global $$m;
            }
            $string = 
preg_replace("/&([a-zA-Z_]+)\.([a-zA-Z_]+);/sei", "\${\\1}['\\2']", 
$string);
        }
        return $string;
    }

Which I use as such:

    print parserxml($template, $article);

You may also note that there is support for "RXML 2.0" in that 
function as well, which used a slightly different syntax that could 
define scope as well:

    $template = "<h2>#headline#</h2>&user.name;"

The second part would globalize $user and replace that with 
$user["name"].



-- 
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