Intepolation within string in a variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Aldred

    #1

    Intepolation within string in a variable?

    Evening all!

    Sorry if its a faq of some sort, but I haven't found an answer to this as
    yet.

    I'm trying to write a function which will go and get data out of a database
    and lay it out. The awkward bit is that I want to be able to vary the
    layout: I want to be able to pass a string to the function to define the
    layout.

    So in the simplest form of the situation (ignoring the database bit) I want
    to be able to do something like

    function myshow($x)
    { $Text = "wibble";
    echo $x;
    }

    myshow("This is \$Text \n");
    myshow("And so is this.... \$Text !!!")

    and get the output

    This is wibble
    And so is this.... wibble !!!


    I can't get it to work - is it possible, perhaps with some manipulation of
    the syntax of the string?

    --
    David Aldred
  • Janwillem Borleffs

    #2
    Re: Intepolation within string in a variable?

    David Aldred wrote:[color=blue]
    > I can't get it to work - is it possible, perhaps with some
    > manipulation of the syntax of the string?[/color]

    This is one of the rare situations where eval is useful:

    function myshow($x) {
    $Text = "wibble";
    eval("print \"$x\";");
    }


    JW



    Comment

    • Jan Pieter Kunst

      #3
      Re: Intepolation within string in a variable?

      David Aldred wrote:
      [color=blue]
      > So in the simplest form of the situation (ignoring the database bit) I want
      > to be able to do something like
      >
      > function myshow($x)
      > { $Text = "wibble";
      > echo $x;
      > }
      >
      > myshow("This is \$Text \n");
      > myshow("And so is this.... \$Text !!!")
      >
      > and get the output
      >
      > This is wibble
      > And so is this.... wibble !!!
      >
      >
      > I can't get it to work - is it possible, perhaps with some manipulation of
      > the syntax of the string?[/color]

      The eval() solution is cool. This one, though less elegant, also works:

      function myshow($x)
      { $Text = "wibble";
      echo str_replace("\$ Text", $Text, $x);
      }

      JP

      --
      Sorry, <devnull@cauce. org> is a spam trap.
      Real e-mail address unavailable. 5000+ spams per month.

      Comment

      • NC

        #4
        Re: Intepolation within string in a variable?

        David Aldred wrote:[color=blue]
        >
        > I'm trying to write a function which will go and get data out
        > of a database and lay it out. The awkward bit is that I want
        > to be able to vary the layout: I want to be able to pass a
        > string to the function to define the layout.
        >
        > So in the simplest form of the situation (ignoring the database
        > bit) I want to be able to do something like
        >
        > function myshow($x)
        > { $Text = "wibble";
        > echo $x;
        > }
        >
        > myshow("This is \$Text \n");
        > myshow("And so is this.... \$Text !!!")
        >
        > and get the output
        >
        > This is wibble
        > And so is this.... wibble !!![/color]

        You can use eval() as Janwillem suggested, or pass to the myshow()
        function a string containing a placeholder:

        function myshow($x) {
        echo str_replace('## #WIBBLE###', 'wibble', $x);
        }
        myshow("This is ###WIBBLE### \n");
        myshow("And so is this.... ###WIBBLE### !!!")

        Cheers,
        NC

        Comment

        • John Bokma

          #5
          Re: Intepolation within string in a variable?

          Jan Pieter Kunst wrote:
          [color=blue]
          > The eval() solution is cool. This one, though less elegant, also
          > works:
          >
          > function myshow($x)
          > { $Text = "wibble";
          > echo str_replace("\$ Text", $Text, $x);
          > }[/color]

          There are many, many situations the above is *more* elegant.

          --
          John MexIT: http://johnbokma.com/mexit/
          personal page: http://johnbokma.com/
          Experienced programmer available: http://castleamber.com/
          Happy Customers: http://castleamber.com/testimonials.html

          Comment

          • Brion Vibber

            #6
            Re: Intepolation within string in a variable?

            Janwillem Borleffs wrote:[color=blue]
            > This is one of the rare situations where eval is useful:
            >
            > function myshow($x) {
            > $Text = "wibble";
            > eval("print \"$x\";");
            > }[/color]

            Never *ever* use a function like that on client-supplied data that
            hasn't been very carefully validated, or you may find with something
            like this:

            myshow('"; exec("arbitrary shell commands"); print "');

            -- brion vibber (brion @ pobox.com)

            Comment

            • nr@familyaldred.org.uk

              #7
              Re: Intepolation within string in a variable?

              Brion Vibber wrote:[color=blue]
              > Janwillem Borleffs wrote:[color=green]
              > > This is one of the rare situations where eval is useful:
              > >
              > > function myshow($x) {
              > > $Text = "wibble";
              > > eval("print \"$x\";");
              > > }[/color]
              >[/color]
              Thanks for that - I think it meets the need here very nicely!
              [color=blue]
              > Never *ever* use a function like that on client-supplied data that
              > hasn't been very carefully validated,[/color]

              Good point. In this case the layout string passed to the function will
              always come from the script itself, and the actual data from the
              (already cleaned up) database fields, so should be safe.

              Again, thanks for all the help.

              --
              David Aldred

              Comment

              • Chung Leong

                #8
                Re: Intepolation within string in a variable?

                "Jan Pieter Kunst" <devnull@cauce. org> wrote in message
                news:42375567$0 $28994$e4fe514c @news.xs4all.nl ...[color=blue]
                > David Aldred wrote:
                >[color=green]
                > > So in the simplest form of the situation (ignoring the database bit) I[/color][/color]
                want[color=blue][color=green]
                > > to be able to do something like
                > >
                > > function myshow($x)
                > > { $Text = "wibble";
                > > echo $x;
                > > }
                > >
                > > myshow("This is \$Text \n");
                > > myshow("And so is this.... \$Text !!!")
                > >
                > > and get the output
                > >
                > > This is wibble
                > > And so is this.... wibble !!!
                > >
                > >
                > > I can't get it to work - is it possible, perhaps with some manipulation[/color][/color]
                of[color=blue][color=green]
                > > the syntax of the string?[/color]
                >
                > The eval() solution is cool. This one, though less elegant, also works:
                >
                > function myshow($x)
                > { $Text = "wibble";
                > echo str_replace("\$ Text", $Text, $x);
                > }[/color]

                The eval() solution is not cool at all. That's precisely what the santy worm
                exploited.

                strtr() is probably a better solution here, as it can make mutiple
                replacement in one call.


                Comment

                • Jan Pieter Kunst

                  #9
                  Re: Intepolation within string in a variable?

                  Chung Leong wrote:
                  [color=blue]
                  > The eval() solution is not cool at all. That's precisely what the santy worm
                  > exploited.[/color]

                  Well, as long as what is eval()'d is known beforehand to be safe, i.e.
                  no user-submitted data, it it really that bad? Surely that leaves no
                  room for exploits?

                  Or am I overlooking something?

                  JP

                  --
                  Sorry, <devnull@cauce. org> is a spam trap.
                  Real e-mail address unavailable. 5000+ spams per month.

                  Comment

                  • Brion Vibber

                    #10
                    Re: Intepolation within string in a variable?

                    Jan Pieter Kunst wrote:[color=blue]
                    > Chung Leong wrote:[color=green]
                    >> The eval() solution is not cool at all. That's precisely what the
                    >> santy worm exploited.[/color]
                    >
                    > Well, as long as what is eval()'d is known beforehand to be safe, i.e.
                    > no user-submitted data, it it really that bad? Surely that leaves no
                    > room for exploits?[/color]

                    It's a matter of risk management. Knowing that human software developers
                    _do_ make mistakes, what's the likely result of an error in input
                    verification?

                    If you make a mistake with str_replace() or strtr(), you'll just get
                    incorrect output. Depending on the use of the output JavaScript or SQL
                    injection might be possible; other uses might not be exploitable to any
                    significant end.

                    If you make a mistake with eval() and the data is not formatted safely
                    after all, the possible results are very high-danger: arbitrary local
                    code execution as web server user, no matter what the replaced text is
                    to be used for.

                    -- brion vibber (brion @ pobox.com)

                    Comment

                    Working...