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 Newsgroups: comp.lang.php Subject: Re: variable replacement in string Date: Fri, 11 May 2012 15:48:55 +0200 Lines: 103 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: individual.net 6vhdQGfwZ8s5XHwPK/crEQZnjLiOJZi1srxdJPwQuju1G8I4RUA1XqLXvfn6jwFaU= X-Orig-Path: mr Cancel-Lock: sha1:r/ndCY53WdxZJL+TaivwSYBxEmY= User-Agent: MT-NewsWatcher/3.5.2 (Intel Mac OS X) X-Face: $@,Vfa$,)%=Qa7L]y)&oZj_\EiHc}}Af0Bei"4a_%)"c6TQ+P/:53>;PNGuWUmkqyeN-qM65foJ[;T_(k;>]&G\T4Lhm:2 ujye2_,iUJFE;NZn>y;.|-hl7g~bIOF1qG\o, MuahMan, Redjak, isquat@gmail.com, Robert Whelan, jt2002a@hotmail.com Xref: csiph.com comp.lang.php:5963 In article , "M. Strobel" wrote: > Am 10.05.2012 16:10, schrieb Sandman: > > In article , > > "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. > > > > 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 = "

#headline#

> > #date#, #category#"; > > > > 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 = "

#headline#

&user.name;" > > > > The second part would globalize $user and replace that with > > $user["name"]. > > This type of solution was commented already with a one-liner: create my own > variable > system? > > Thank you for this complete proposal. You are the first to know what I am > missing. Can't say I understand what you want if the proposed methods doesn't float your boat. The above code can easily be tweaked to handle PHP-style variables (like print parsevar('Hello $t, how are you', $vars);) This, no need for your "own variable system". Or rather, the variable system would emulate that of PHP. In the end, it's all text strings that are being processed, how you insert variables into the string is just a matter of preference. -- Sandman[.net]