Resolving {$var} from within a string...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Jones

    #1

    Resolving {$var} from within a string...

    If i use

    <?
    $x = 4 ;
    echo "This is the value of x: {$x}" ;
    ?>

    i will obviously get the output
    This is the value of x: 4

    But when i have a string, in an SQL varchar field, such as
    kra.php?EID={$e id}

    I can then do $href = db_query($qry,0 ,"href") to get the SQL data into
    $href, but when i echo $href, the $eid comes out as plain-text and
    doesn't resolve to the current value of $eid.

    Is it possible to force PHP to attempt to resolve any embedded $vars
    within a string?
    i.e. so when i output $href, i get
    kra.php?EID=1

    Thanks

  • Toby A Inkster

    #2
    Re: Resolving {$var} from within a string...

    Chris Jones wrote:
    Is it possible to force PHP to attempt to resolve any embedded $vars
    within a string?
    No.

    But look at sprintf().

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

    * = I'm getting there!

    Comment

    • Kimmo Laine

      #3
      Re: Resolving {$var} from within a string...

      "Toby A Inkster" <usenet200703@t obyinkster.co.u kwrote in message
      news:p72cf4-4kp.ln1@ophelia .g5n.co.uk...
      Chris Jones wrote:
      >
      >Is it possible to force PHP to attempt to resolve any embedded $vars
      >within a string?
      >
      No.
      Wrong. It is possible with eval(), but it's strongly advaised not to use
      this approach:


      --
      "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
      http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
      spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


      Comment

      • Chris Jones

        #4
        Re: Resolving {$var} from within a string...

        On 16 Apr, 13:24, "Kimmo Laine" <s...@outolempi .netwrote:
        "Toby A Inkster" <usenet200...@t obyinkster.co.u kwrote in messagenews:p72 cf4-4kp.ln1@ophelia .g5n.co.uk...
        >
        Chris Jones wrote:
        Cracked it.... the easiest way surely is just

        $eid = $_GET['EID'];
        $href = str_replace("{$ eid}","{$eid}", db_result($qry, 0,"href");

        :-)
        >
        Is it possible to force PHP to attempt to resolve any embedded $vars
        within a string?
        >
        No.
        >
        Wrong. It is possible with eval(), but it's strongly advaised not to use
        this approach:http://fi2.php.net/manual/en/function.eval.php
        >
        --
        "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpkhttp://outolempi.net/ahdistus/- Satunnaisesti päivittyvä nettisarjis
        s...@outolempi. net | rot13(x...@bhgb yrzcv.arg)

        Comment

        • Bocah Sableng

          #5
          Re: Resolving {$var} from within a string...

          On Apr 18, 2:57 pm, Chris Jones <chris...@gmail .comwrote:
          $eid = $_GET['EID'];
          $href = str_replace("{$ eid}","{$eid}", db_result($qry, 0,"href");
          >
          Beware of case-sensitive behaviour of str_replace. Perhaps you should
          replace str_replace() with str_ireplace() or just change the variable
          name to uppercase. And dollar sign inside double quotes replaced with
          its variable value.

          $href = str_ireplace("{ $eid}", $eid, db_result($qry, 0,"href");
          or
          $href = str_replace("{\ $EID}", $eid, db_result($qry, 0,"href");

          For a better approach, consider using preg_replace() with pattern like
          "/\{\\\$EID\}/". Because preg_replace() can accept array as parameter,
          so you can replace multiple variables in single step.
          Another method is using preg_replace_ca llback() with reusable callback
          function.

          HTH

          Comment

          Working...