PHP equivalent of TCL command "subst"?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    #1

    PHP equivalent of TCL command "subst"?

    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'} ,
    $successMsgArra y;
    if (is_array($_POS T) && @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',
    $successMsgArra y[$section][$action]);
    $junk = eval('$' . 'msg = $msg;');
    return $msg;

    }
    [/PHP]

    Thanx
    Phil
  • Chung Leong

    #2
    Re: PHP equivalent of TCL command "subst&quo t;?


    "Phil Powell" <soazine@erols. com> wrote in message
    news:1cdca2a7.0 406221531.52d7d 184@posting.goo gle.com...[color=blue]
    > 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'} ,
    > $successMsgArra y;
    > if (is_array($_POS T) && @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',
    > $successMsgArra y[$section][$action]);
    > $junk = eval('$' . 'msg = $msg;');
    > return $msg;
    >
    > }
    > [/PHP][/color]

    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?


    Comment

    • Phil Powell

      #3
      Re: PHP equivalent of TCL command &quot;subst&quo t;?

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

      [PHP]
      $msg = preg_replace('/#([^#]+)#/e', '$$1',
      $successMsgArra y[$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" <chernyshevsky@ hotmail.com> wrote in message news:<MeCdnSTbr uXuY0XdRVn-uw@comcast.com> ...[color=blue]
      > "Phil Powell" <soazine@erols. com> wrote in message
      > news:1cdca2a7.0 406221531.52d7d 184@posting.goo gle.com...[color=green]
      > > 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'} ,
      > > $successMsgArra y;
      > > if (is_array($_POS T) && @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',
      > > $successMsgArra y[$section][$action]);
      > > $junk = eval('$' . 'msg = $msg;');
      > > return $msg;
      > >
      > > }
      > > [/PHP][/color]
      >
      > 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?[/color]

      Comment

      • Chung Leong

        #4
        Re: PHP equivalent of TCL command &quot;subst&quo t;?

        "Phil Powell" <soazine@erols. com> wrote in message
        news:1cdca2a7.0 406231136.4e847 b3d@posting.goo gle.com...[color=blue]
        > I'm sorry I thought I was. Perhaps this might be of more use then:
        >
        > [PHP]
        > $msg = preg_replace('/#([^#]+)#/e', '$$1',
        > $successMsgArra y[$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.
        >[/color]

        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_ca llback('/#([^#]+)#/e', 'Global',
        $successMsgArra y[$section][$action]);


        Comment

        Working...