Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post

    Passing "{" as variable in php

    Hi,
    I'm a bit puzzled by this, how do I pass a "{" (curly brackets) in php?

    I'm using smarty and I wanted to define a variable that has some smarty variables in it but they use "{" and it gets translated into html when passed.

    Here's my current code:
    PHP Code:
    if ($modrewrite == 1) {
        
    $url_link '/play/game-{$randomgamId}/{$randomgamName|replace:" ":"-"}.html';
        } else {
        
    $url_link '/play.php?gamId={$randomgamId}';
    }
    $smarty->assign('url_link'$url_link); 
    {$randomgamId} is a variable used by smarty.

    Any help is very much appreciated....

    Thanks
    Last edited by forpals; 03-14-2008 at 05:57 PM.

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Try escaping it with a backslash ( \ )

    in other words:

    $url_link = '/play.php?gamId=\{$randomgamId\}';

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    PHP variables aren't parsed if they aren't in double quotes.
    PHP Code:
    if ($modrewrite == 1) {
        
    $url_link "/play/game-{$randomgamId}/{$randomgamName|replace:' ':'-'}.html";
        } else {
        
    $url_link "/play.php?gamId={$randomgamId}";
    }
    $smarty->assign('url_link'$url_link); 
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    New Coder
    Join Date
    Nov 2005
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by whizard View Post
    Try escaping it with a backslash ( \ )

    in other words:

    $url_link = '/play.php?gamId=\{$randomgamId\}';

    HTH
    Dan
    Hi. Thanks for the quick reply.
    I tried as you had suggested (see below):
    Code:
    if ($modrewrite == 1) {
    	$url_link = '/play/game-\{$randomgamId\}/\{$randomgamName|replace:" ":"-"\}.html';
    	} else {
    	$url_link = '/play.php?gamId=\{$randomgamId\}';
    }
    $smarty->assign('url_link', $url_link);
    Unfortunately... It's still getting passed as this on the link:
    Code:
    http://website.url/play/game-/%7B$randomgamId/%7D//%7B$randomgamName%7Creplace:
    Any idea?
    Thanks

  • #5
    New Coder
    Join Date
    Nov 2005
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by _Aerospace_Eng_ View Post
    PHP variables aren't parsed if they aren't in double quotes.
    PHP Code:
    if ($modrewrite == 1) {
        
    $url_link "/play/game-{$randomgamId}/{$randomgamName|replace:' ':'-'}.html";
        } else {
        
    $url_link "/play.php?gamId={$randomgamId}";
    }
    $smarty->assign('url_link'$url_link); 
    Hi. I tried as you had suggested and now I get the following error:
    Code:
    Parse error: syntax error, unexpected '|', expecting '}' in C:\xampp\htdocs\test.com\index.php on line 38
    which I suspect is from this line:
    Code:
    $url_link = "/play/game-{$randomgamId}/{$randomgamName|replace:' ':'-'}.html";
    Thanks for the help...

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    the point of using a templating language is so you don't have to mess about with things like this, rethink your design, it makes no sense. Passing Smarty syntax to smarty won't result in it being parsed by Smarty.

    take a look at what Smarty lets you do with custom functions and modifiers, and see if that would make things easier for you.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #7
    New Coder
    Join Date
    Nov 2005
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by GJay View Post
    the point of using a templating language is so you don't have to mess about with things like this, rethink your design, it makes no sense. Passing Smarty syntax to smarty won't result in it being parsed by Smarty.

    take a look at what Smarty lets you do with custom functions and modifiers, and see if that would make things easier for you.
    Actually, you're right... I was trying to find a shortcut and not go back to the drawing board, but I think that leaves me with no choice Off to recoding I go... Problem was I didn't take mod_rewrite in consideration when I developed the script but now it's a must have... Thanks for setting me straight


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •