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
Switch to threaded view of this topic Create a new topic Submit Reply
variable replacement in string [message #178002] Thu, 10 May 2012 12:44 Go to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.
Re: variable replacement in string [message #178003 is a reply to message #178002] Thu, 10 May 2012 13:00 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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.

what is wrong with printf("the problem is %s \n",$t);


or

echo ("the problem is".$t);

???


--
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.
Re: variable replacement in string [message #178004 is a reply to message #178002] Thu, 10 May 2012 13:05 Go to previous messageGo to next message
Goran is currently offline  Goran
Messages: 38
Registered: January 2011
Karma: 0
Member
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',
));
Re: variable replacement in string [message #178005 is a reply to message #178003] Thu, 10 May 2012 13:07 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 10.05.2012 15:00, schrieb The Natural Philosopher:
> 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.
>
> what is wrong with printf("the problem is %s \n",$t);

relating %s to $t is extra work.

>
>
> or
>
> echo ("the problem is".$t);
>

or

echo 'the problem is ', $t;

be sure I can do it in code. In Tcl, interactive session:

strobel@s114-intel:~> tclsh
% set t " - solved - "
- solved -
% set msg {the problem is $t}
the problem is $t
% puts $msg
the problem is $t
% puts [subst $msg]
the problem is - solved -
%

/Str.
Re: variable replacement in string [message #178006 is a reply to message #178004] Thu, 10 May 2012 13:08 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.
Re: variable replacement in string [message #178007 is a reply to message #178006] Thu, 10 May 2012 13:11 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
M. Strobel 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.
>
if you want to program in tcl., i suggest you use tcl.

your example
$t = ' - solved - ';
$msg = 'The problem is $t';

should be
$t = ' - solved - ';
$msg = 'The problem is'.$t;
echo($msg);


--
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.
Re: variable replacement in string [message #178008 is a reply to message #178006] Thu, 10 May 2012 13:13 Go to previous messageGo to next message
Goran is currently offline  Goran
Messages: 38
Registered: January 2011
Karma: 0
Member
On 10.5.2012 15:08, M. Strobel wrote:
> yeah, create my own variable system. No easier way?

Yes of course, there are some alternative techniques like http://bit.ly/NVYN
Re: variable replacement in string [message #178009 is a reply to message #178002] Thu, 10 May 2012 13:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/10/2012 8:44 AM, 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.

There's also preg_replace(), but that's a bit of overkill for something
so simple.

str_replace() works fine, especially since both the search and replace
parameters can be arrays. But when using message templates, I don't use
variable names - it ties the template tightly to the code.

Rather, I use templates like:

$t = ' - solved - ';
$template = 'The problem is %PROB_STAT%';
echo str_replace('%PROB_STAT%', $t, $template);

Or, if you had the possibility of more than one string to substitute:

$search = array('$PROB_STAT%');
$replace = array($t);
echo str_replace('%PROB_STAT%', $t, $template);

Your method requires the template know that $t is the value in the
program, and requires the program to ensure $t is defined properly in
this scope. Neither is a good idea.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: variable replacement in string [message #178010 is a reply to message #178002] Thu, 10 May 2012 14:04 Go to previous messageGo to next message
Jeff North is currently offline  Jeff North
Messages: 58
Registered: November 2010
Karma: 0
Member
On Thu, 10 May 2012 14:44:08 +0200, in comp.lang.php "M. Strobel"
<sorry_no_mail_here(at)nowhere(dot)dee>
<a11rgoFph0U1(at)mid(dot)uni-berlin(dot)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}';
Re: variable replacement in string [message #178011 is a reply to message #178002] Thu, 10 May 2012 14:10 Go to previous messageGo to next message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma: 0
Member
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"].



--
Sandman[.net]
Re: variable replacement in string [message #178013 is a reply to message #178007] Thu, 10 May 2012 16:21 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 10.05.2012 15:11, schrieb The Natural Philosopher:

> if you want to program in tcl., i suggest you use tcl.
>
> your example
> $t = ' - solved - ';
> $msg = 'The problem is $t';
>
> should be
> $t = ' - solved - ';
> $msg = 'The problem is'.$t;
> echo($msg);

You cannot miss something you do not know it exists.

/Str.
Re: variable replacement in string [message #178014 is a reply to message #178008] Thu, 10 May 2012 16:23 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 10.05.2012 15:13, schrieb Goran:
> On 10.5.2012 15:08, M. Strobel wrote:
>> yeah, create my own variable system. No easier way?
>
> Yes of course, there are some alternative techniques like http://bit.ly/NVYN

Haha. I read Stanislav Lem in the seventies, far more imagination...

/Str.
Re: variable replacement in string [message #178015 is a reply to message #178009] Thu, 10 May 2012 16:29 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 10.05.2012 15:14, schrieb Jerry Stuckle:
> On 5/10/2012 8:44 AM, 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.
>
> There's also preg_replace(), but that's a bit of overkill for something so simple.
>
> str_replace() works fine, especially since both the search and replace parameters can
> be arrays. But when using message templates, I don't use variable names - it ties
> the template tightly to the code.
>
> Rather, I use templates like:
>
> $t = ' - solved - ';
> $template = 'The problem is %PROB_STAT%';
> echo str_replace('%PROB_STAT%', $t, $template);
>
> Or, if you had the possibility of more than one string to substitute:
>
> $search = array('$PROB_STAT%');
> $replace = array($t);
> echo str_replace('%PROB_STAT%', $t, $template);
>
> Your method requires the template know that $t is the value in the program, and
> requires the program to ensure $t is defined properly in this scope. Neither is a
> good idea.
>
I see the problem of the undefined $t.

Think of my method as "do what double quotes do".

It may well not exist, maybe I file a feature request.

/Str.
Re: variable replacement in string [message #178016 is a reply to message #178010] Thu, 10 May 2012 16:32 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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(at)nowhere(dot)dee>
> <a11rgoFph0U1(at)mid(dot)uni-berlin(dot)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.
Re: variable replacement in string [message #178017 is a reply to message #178011] Thu, 10 May 2012 16:37 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.

/Str.
Re: variable replacement in string [message #178018 is a reply to message #178017] Thu, 10 May 2012 16:38 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 10.05.2012 18:37, schrieb M. Strobel:
> 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.

oops, wrong translation, must be "who knows" ^^^^^^^
Re: variable replacement in string [message #178019 is a reply to message #178015] Thu, 10 May 2012 18:12 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/10/2012 12:29 PM, M. Strobel wrote:
> Am 10.05.2012 15:14, schrieb Jerry Stuckle:
>> On 5/10/2012 8:44 AM, 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.
>>
>> There's also preg_replace(), but that's a bit of overkill for something so simple.
>>
>> str_replace() works fine, especially since both the search and replace parameters can
>> be arrays. But when using message templates, I don't use variable names - it ties
>> the template tightly to the code.
>>
>> Rather, I use templates like:
>>
>> $t = ' - solved - ';
>> $template = 'The problem is %PROB_STAT%';
>> echo str_replace('%PROB_STAT%', $t, $template);
>>
>> Or, if you had the possibility of more than one string to substitute:
>>
>> $search = array('$PROB_STAT%');
>> $replace = array($t);
>> echo str_replace('%PROB_STAT%', $t, $template);
>>
>> Your method requires the template know that $t is the value in the program, and
>> requires the program to ensure $t is defined properly in this scope. Neither is a
>> good idea.
>>
> I see the problem of the undefined $t.
>
> Think of my method as "do what double quotes do".
>
> It may well not exist, maybe I file a feature request.
>
> /Str.

No, you miss the point. You are also now closely coupling your template
to your code. Such coupling is not good - it severely limits possibilities.

For instance, let's say you want to use the same message another place
in your code - but you've already used $t. Do you rewrite the entire
routine? Or maybe you want to use your templates somewhere completely
different.

Tight coupling is bad because it creates more (and often unnecessary)
dependencies. Loose coupling limits dependencies, and fewer
dependencies means fewer errors crop into the code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: variable replacement in string [message #178027 is a reply to message #178013] Thu, 10 May 2012 20:50 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
M. Strobel wrote:
> Am 10.05.2012 15:11, schrieb The Natural Philosopher:
>
>> if you want to program in tcl., i suggest you use tcl.
>>
>> your example
>> $t = ' - solved - ';
>> $msg = 'The problem is $t';
>>
>> should be
>> $t = ' - solved - ';
>> $msg = 'The problem is'.$t;
>> echo($msg);
>
> You cannot miss something you do not know it exists.
>

Of course you can. Don't be silly.

I've missed loads of things that I didnt know about.

Like the flight to Chicago. I've missed every single flight to chicago,
ever. Most before I even knew there was one. :-)



> /Str.
>


--
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.
Re: variable replacement in string [message #178029 is a reply to message #178027] Thu, 10 May 2012 21:06 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/10/2012 4:50 PM, The Natural Philosopher wrote:
> M. Strobel wrote:
>> Am 10.05.2012 15:11, schrieb The Natural Philosopher:
>>
>>> if you want to program in tcl., i suggest you use tcl.
>>>
>>> your example
>>> $t = ' - solved - ';
>>> $msg = 'The problem is $t';
>>>
>>> should be
>>> $t = ' - solved - ';
>>> $msg = 'The problem is'.$t;
>>> echo($msg);
>>
>> You cannot miss something you do not know it exists.
>>
>
> Of course you can. Don't be silly.
>
> I've missed loads of things that I didnt know about.
>
> Like the flight to Chicago. I've missed every single flight to chicago,
> ever. Most before I even knew there was one. :-)
>
>
>
>> /Str.
>>
>
>

Which makes the people of Chicago VERY HAPPY!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: variable replacement in string [message #178032 is a reply to message #178029] Thu, 10 May 2012 22:31 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 5/10/2012 2:06 PM, Jerry Stuckle wrote:
> On 5/10/2012 4:50 PM, The Natural Philosopher wrote:
>> M. Strobel wrote:
>>> Am 10.05.2012 15:11, schrieb The Natural Philosopher:
>>>
>>>> if you want to program in tcl., i suggest you use tcl.
>>>>
>>>> your example
>>>> $t = ' - solved - ';
>>>> $msg = 'The problem is $t';
>>>>
>>>> should be
>>>> $t = ' - solved - ';
>>>> $msg = 'The problem is'.$t;
>>>> echo($msg);
>>>
>>> You cannot miss something you do not know it exists.
>>>
>>
>> Of course you can. Don't be silly.
>>
>> I've missed loads of things that I didnt know about.
>>
>> Like the flight to Chicago. I've missed every single flight to chicago,
>> ever. Most before I even knew there was one. :-)
>>
>>
>>
>>> /Str.
>>>
>>
>>
>
> Which makes the people of Chicago VERY HAPPY!
>

Time to get the microwave popcorn ready!
Re: variable replacement in string [message #178035 is a reply to message #178032] Fri, 11 May 2012 00:08 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Scott Johnson wrote:
> On 5/10/2012 2:06 PM, Jerry Stuckle wrote:
>> On 5/10/2012 4:50 PM, The Natural Philosopher wrote:
>>> M. Strobel wrote:
>>>> Am 10.05.2012 15:11, schrieb The Natural Philosopher:
>>>>
>>>> > if you want to program in tcl., i suggest you use tcl.
>>>> >
>>>> > your example
>>>> > $t = ' - solved - ';
>>>> > $msg = 'The problem is $t';
>>>> >
>>>> > should be
>>>> > $t = ' - solved - ';
>>>> > $msg = 'The problem is'.$t;
>>>> > echo($msg);
>>>>
>>>> You cannot miss something you do not know it exists.
>>>>
>>>
>>> Of course you can. Don't be silly.
>>>
>>> I've missed loads of things that I didnt know about.
>>>
>>> Like the flight to Chicago. I've missed every single flight to chicago,
>>> ever. Most before I even knew there was one. :-)
>>>
>>>
>>>
>>>> /Str.
>>>>
>>>
>>>
>>
>> Which makes the people of Chicago VERY HAPPY!
>>
>
> Time to get the microwave popcorn ready!

Hardly,. His lord High Stuckupness has been in my killfile a LONG time,
as he never says anything worth reading - as evinced by this
particularly puerile offering.


--
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.
Re: variable replacement in string [message #178038 is a reply to message #178016] Fri, 11 May 2012 02:30 Go to previous messageGo to next message
Jeff North is currently offline  Jeff North
Messages: 58
Registered: November 2010
Karma: 0
Member
On Thu, 10 May 2012 18:32:58 +0200, in comp.lang.php "M. Strobel"
<sorry_no_mail_here(at)nowhere(dot)dee>
<a128trFt9iU1(at)mid(dot)uni-berlin(dot)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(at)nowhere(dot)dee>
> | > <a11rgoFph0U1(at)mid(dot)uni-berlin(dot)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;
Re: variable replacement in string [message #178039 is a reply to message #178038] Fri, 11 May 2012 07:32 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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(at)nowhere(dot)dee>
> <a128trFt9iU1(at)mid(dot)uni-berlin(dot)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(at)nowhere(dot)dee>
>> | > <a11rgoFph0U1(at)mid(dot)uni-berlin(dot)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.
Re: variable replacement in string [message #178040 is a reply to message #178002] Fri, 11 May 2012 07:58 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
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
--
Re: variable replacement in string [message #178041 is a reply to message #178040] Fri, 11 May 2012 08:31 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.
Re: variable replacement in string [message #178052 is a reply to message #178017] Fri, 11 May 2012 13:48 Go to previous messageGo to next message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma: 0
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]
Re: variable replacement in string [message #178053 is a reply to message #178006] Fri, 11 May 2012 14:06 Go to previous message
Sandman is currently offline  Sandman
Messages: 32
Registered: August 2011
Karma: 0
Member
In article <a11su1F3j9U2(at)mid(dot)uni-berlin(dot)de>,
"M. Strobel" <sorry_no_mail_here(at)nowhere(dot)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]
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Fri Sep 20 04:13:20 GMT 2024

Total time taken to generate the page: 0.02991 seconds