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 #178052 is a reply to message #178017] Fri, 11 May 2012 13:48 Go to previous messageGo to previous message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma:
Member
In article <a1295sFv8lU1(at)mid(dot)uni-berlin(dot)de>,
"M. Strobel" <sorry_no_mail_here(at)nowhere(dot)dee> wrote:

> Am 10.05.2012 16:10, schrieb Sandman:
>> In article <a11rgoFph0U1(at)mid(dot)uni-berlin(dot)de>,
>> "M. Strobel" <sorry_no_mail_here(at)nowhere(dot)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"].
>
> 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]
[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: Sun Dec 01 04:24:39 GMT 2024

Total time taken to generate the page: 0.05347 seconds