Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #5913 > unrolled thread
| Started by | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| First post | 2012-05-10 14:44 +0200 |
| Last post | 2012-05-11 10:31 +0200 |
| Articles | 7 on this page of 27 — 8 participants |
Back to article view | Back to comp.lang.php
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 2 of 2 — ← Prev page 1 [2]
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Date | 2012-05-11 09:32 +0200 |
| Message-ID | <a13tl7F15kU1@mid.uni-berlin.de> |
| In reply to | #5949 |
Am 11.05.2012 04:30, schrieb Jeff North:
> 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;
strobel@s114-intel:~> php -a
Interactive shell
php > $t = ' solved!!';
php > $msg = 'The problem is {$t}';
php > echo $msg;
The problem is {$t}
php > $msg = 'The problem is ${t}';
php > echo $msg;
The problem is ${t}
php > $msg = "The problem is ${t}";
php > echo $msg;
The problem is solved!!
php >
You missed something. Only double quotes do it. When you take $msg from a file you
won't be able to do the var replacement.
/Str.
[toc] | [prev] | [next] | [standalone]
| From | Sandman <mr@sandman.net> |
|---|---|
| Date | 2012-05-10 16:10 +0200 |
| Message-ID | <mr-739F0E.16101410052012@News.Individual.NET> |
| In reply to | #5913 |
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]
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Date | 2012-05-10 18:37 +0200 |
| Message-ID | <a1295sFv8lU1@mid.uni-berlin.de> |
| In reply to | #5922 |
Am 10.05.2012 16:10, schrieb Sandman:
> 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"].
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.
/Str.
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Date | 2012-05-10 18:38 +0200 |
| Message-ID | <a1298uFv8lU2@mid.uni-berlin.de> |
| In reply to | #5928 |
Am 10.05.2012 18:37, schrieb M. Strobel:
> Am 10.05.2012 16:10, schrieb Sandman:
>> 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"].
>
> 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.
oops, wrong translation, must be "who knows" ^^^^^^^
[toc] | [prev] | [next] | [standalone]
| From | Sandman <mr@sandman.net> |
|---|---|
| Date | 2012-05-11 15:48 +0200 |
| Message-ID | <mr-DCBE17.15485511052012@News.Individual.NET> |
| In reply to | #5928 |
In article <a1295sFv8lU1@mid.uni-berlin.de>,
"M. Strobel" <sorry_no_mail_here@nowhere.dee> wrote:
> Am 10.05.2012 16:10, schrieb Sandman:
> > 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"].
>
> 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]
[toc] | [prev] | [next] | [standalone]
| From | "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> |
|---|---|
| Date | 2012-05-11 09:58 +0200 |
| Message-ID | <joigqm$613$1@dont-email.me> |
| In reply to | #5913 |
El 10/05/2012 14:44, M. Strobel escribió/wrote: > 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. You haven't really explained why those three methods fail to meet your requirements or how other languages succeed. If we don't know that, any other proposals are likely to be a waste of time. If you are just looking for some HTML template engine, you have many third-party libraries to choose, from Smarty to Twig. -- -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain -- Mi sitio sobre programación web: http://borrame.com -- Mi web de humor satinado: http://www.demogracia.com --
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Date | 2012-05-11 10:31 +0200 |
| Message-ID | <a14134Fq03U1@mid.uni-berlin.de> |
| In reply to | #5951 |
Am 11.05.2012 09:58, schrieb "Álvaro G. Vicario": > El 10/05/2012 14:44, M. Strobel escribió/wrote: >> 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. > > You haven't really explained why those three methods fail to meet your requirements > or how other languages succeed. If we don't know that, any other proposals are likely > to be a waste of time. I just want to be sure I did not overlook some possibility, the quest for the truth. /Str.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.php
csiph-web