By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

PHP equivalent of TCL command "subst"?

P: n/a
I need to find a PHP equivalent to a very familiar TCL command "subst"
(see http://www.hume.com/html84/mann/subst.html ).

This command will take a string and evaluate anything TCL inside of it
and return values.

for example, if this were my line:

[TCL]
set word {World}
set line {Hello #word#}
regsub -all {#([^#]+)#} $line {$$1} line
return [subst $line]
[/TCL]

My return should be
Hello World

and not
Hello #word#

Nor
Hello $word

Because of a requirement for my app I am having to do the same kind of
substitution variation, however, I am having to do it entirely in PHP.
I don't know of any PHP manual operated command close to TCL's
subst() but I'm open to the idea.

Here is my class method that fails to do so:

[PHP]
function &getSuccessMsg() { // STATIC STRING METHOD
global $section, $action, ${$section . 'LocationPath'},
$successMsgArray;
if (is_array($_POST) && @sizeof($_POST) > 0) foreach ($_POST as $key
=> $val) if (!isset(${$key})) ${$key} = $val;
$locationPath = ($album) ? "${$section . 'LocationPath'}/$album" :
${$section . 'LocationPath'};
$this->fileName = ($image_name) ? $image_name : $this->fileName;
$fileName = $this->fileName; // FOR REASSIGNMENT INTO GLOBALIZED
2-DIM ARRAY FROM PROCEDURAL GLOBAL LIBRARY project_globals.inc.php
$msg = preg_replace('/#([^#]+)#/i', '$$1',
$successMsgArray[$section][$action]);
$junk = eval('$' . 'msg = $msg;');
return $msg;

}
[/PHP]

Thanx
Phil
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
I need to find a PHP equivalent to a very familiar TCL command "subst"
(see http://www.hume.com/html84/mann/subst.html ).

This command will take a string and evaluate anything TCL inside of it
and return values.

for example, if this were my line:

[TCL]
set word {World}
set line {Hello #word#}
regsub -all {#([^#]+)#} $line {$$1} line
return [subst $line]
[/TCL]

My return should be
Hello World

and not
Hello #word#

Nor
Hello $word

Because of a requirement for my app I am having to do the same kind of
substitution variation, however, I am having to do it entirely in PHP.
I don't know of any PHP manual operated command close to TCL's
subst() but I'm open to the idea.

Here is my class method that fails to do so:

[PHP]
function &getSuccessMsg() { // STATIC STRING METHOD
global $section, $action, ${$section . 'LocationPath'},
$successMsgArray;
if (is_array($_POST) && @sizeof($_POST) > 0) foreach ($_POST as $key
=> $val) if (!isset(${$key})) ${$key} = $val;
$locationPath = ($album) ? "${$section . 'LocationPath'}/$album" :
${$section . 'LocationPath'};
$this->fileName = ($image_name) ? $image_name : $this->fileName;
$fileName = $this->fileName; // FOR REASSIGNMENT INTO GLOBALIZED
2-DIM ARRAY FROM PROCEDURAL GLOBAL LIBRARY project_globals.inc.php
$msg = preg_replace('/#([^#]+)#/i', '$$1',
$successMsgArray[$section][$action]);
$junk = eval('$' . 'msg = $msg;');
return $msg;

}
[/PHP]


Why don't you tell us what you're trying to do instead? I'm sure there's a
more elegant way of accomplishing it.

And why are you returning a reference?
Jul 17 '05 #2

P: n/a
I'm sorry I thought I was. Perhaps this might be of more use then:

[PHP]
$msg = preg_replace('/#([^#]+)#/e', '$$1',
$successMsgArray[$section][$action]);
[/PHP]

That is the PHP equivalent of the TCL "subst" command inasmuch as that
in this case it literally replaces any occurrence of #myVar# with
$myVar value.

Phil
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<Me********************@comcast.com>...
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
I need to find a PHP equivalent to a very familiar TCL command "subst"
(see http://www.hume.com/html84/mann/subst.html ).

This command will take a string and evaluate anything TCL inside of it
and return values.

for example, if this were my line:

[TCL]
set word {World}
set line {Hello #word#}
regsub -all {#([^#]+)#} $line {$$1} line
return [subst $line]
[/TCL]

My return should be
Hello World

and not
Hello #word#

Nor
Hello $word

Because of a requirement for my app I am having to do the same kind of
substitution variation, however, I am having to do it entirely in PHP.
I don't know of any PHP manual operated command close to TCL's
subst() but I'm open to the idea.

Here is my class method that fails to do so:

[PHP]
function &getSuccessMsg() { // STATIC STRING METHOD
global $section, $action, ${$section . 'LocationPath'},
$successMsgArray;
if (is_array($_POST) && @sizeof($_POST) > 0) foreach ($_POST as $key
=> $val) if (!isset(${$key})) ${$key} = $val;
$locationPath = ($album) ? "${$section . 'LocationPath'}/$album" :
${$section . 'LocationPath'};
$this->fileName = ($image_name) ? $image_name : $this->fileName;
$fileName = $this->fileName; // FOR REASSIGNMENT INTO GLOBALIZED
2-DIM ARRAY FROM PROCEDURAL GLOBAL LIBRARY project_globals.inc.php
$msg = preg_replace('/#([^#]+)#/i', '$$1',
$successMsgArray[$section][$action]);
$junk = eval('$' . 'msg = $msg;');
return $msg;

}
[/PHP]


Why don't you tell us what you're trying to do instead? I'm sure there's a
more elegant way of accomplishing it.

And why are you returning a reference?

Jul 17 '05 #3

P: n/a
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
I'm sorry I thought I was. Perhaps this might be of more use then:

[PHP]
$msg = preg_replace('/#([^#]+)#/e', '$$1',
$successMsgArray[$section][$action]);
[/PHP]

That is the PHP equivalent of the TCL "subst" command inasmuch as that
in this case it literally replaces any occurrence of #myVar# with
$myVar value.


What I mean was what getSuccessMsg() is supposed to do. Inserting $names
into a string and then expanding them seems rather stupid to me. Why do just
insert the variables directly?

function Global($m) {
return $GLOBALS[$m[1]];
}

$msg = preg_replace_callback('/#([^#]+)#/e', 'Global',
$successMsgArray[$section][$action]);
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.