literal $ character ?

Discussion in 'PHP' started by stats, Feb 20, 2008.

  1. #1
    Hi

    i'm having trouble with reassigning a text variable that has $ in it

    here's the example

    $friendname = "John";
    $bananaprice = 2233;
    $pricelist = "The banana costs \$$bananaprice per a ton";
    
    $message = "Dear $friendname \n";
    $message .= "Below is the pricing you requested \n";
    $message .= $pricelist;
    
    mail("john@john.com", "banana price", $message);
    PHP:
    after sending this email, i get the following

    so it eats up the literal $ character and the following 2 digits of the price .. instead of '$2233' it sends just '33'
    also note, that if i simply do
    echo $bananaprice;
    PHP:
    I will get the expected correct result saying that banana costs $2233 .. so it works with just echoing, but doesnt work when i re-assign the variable

    any solution to this please ?

    Thanks in advance
     
    stats, Feb 20, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I put that exact coding into my own test file and had it email my email and it worked perfectly. If your email is really , I will run a test and make it email that email and you see if it's correct. Only difference we might have is I'm running PHP5, I believe. You could always try (which you should always do, anyways) putting { } around it such as

    $pricelist = "The banana costs \${$bananaprice} per a ton";
    PHP:
    but I see no problems with that script.
     
    zerxer, Feb 20, 2008 IP
  3. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #3
    my server runs on php4

    lemme try the brackets and post an update quick
     
    stats, Feb 20, 2008 IP
  4. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #4
    ok .. my bad ..

    i did some further investigation and the problem is not in the steps that i brought above, but in the next step (still need help with it)

    so the actual code is like this

    $friendname = "John";
    $bananaprice = 2233;
    $pricelist = "The banana costs \$$bananaprice per a ton";
    
    $message = "Dear $friendname \n";
    $message .= "Below is the pricing you requested \n";
    $message .= $pricelist;
    
    $template = file_get_contents("emailtemplate.html");
    $template = preg_replace("^CONTENT^", $message, $template);
    
    mail("john@john.com", "banana price", $template);
    PHP:
    so the problem is when i put that $message thing as a "replace with" in preg_replace function..

    any thoughts please ?

    i know i can simply put all the content of emailtemplate.html inside my PHP and manually put the $message into it, but that's the "not good" way to do it .. so i wanna avoid it
     
    stats, Feb 20, 2008 IP
  5. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can you show me what emailtemplate.html looks like? Why do you have to use preg_replace instead of just str_replace? I think the problem is because preg_replace can take $# and replace it with whatever is in sub classes in your search string. I think for some reason it's trying to replace $22 with the 22nd subclass you have in your search string, which doesn't exist, as you don't have any subclasses anyways. What you can do is put \\\$ in the $pricelist definition so that it actually turns into \$2233 in the end and not just $2233 (and hopefully preg_replace will escape it with that \).
     
    zerxer, Feb 20, 2008 IP
  6. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #6
    the emailtemplate.html is a simple HTML template something like

    <header code goes here with styles and everything>
    CONTENT
    <footer code goes here>
     
    stats, Feb 20, 2008 IP
  7. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #7
    I'd never use
    
    $pricelist = "The banana costs \$$bananaprice per a ton";
    PHP:
    I'd use:

    
    $pricelist = "The banana costs \$".$bananaprice." per a ton";
    
    PHP:
    The echo works for me, not sure about mail() though.
     
    blueparukia, Feb 20, 2008 IP
  8. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #8
    Just use single quotes and escape concenate it,

    
    
    $bill = 123;
    $name = 'Glen';
    
    $message = 'The bill is $'.$bill." $name";
    mail('youremail@email.com', 'Subject', $message) or die ("Mail error");
    echo 'Mail Sent';
    
    PHP:
     
    HuggyStudios, Feb 21, 2008 IP
  9. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I don't think any of those suggestions they gave you will work, because it's not the inserting that is ruining it, it has to be the preg_replace.

    Just use str_replace since you're only replacing one instance of CONTENT and it's not like it's a hard string to replace, don't need regular expressions.

    str_replace("CONTENT", $message, $template);
    PHP:

    EDIT: Or, use my other suggestion.

    $friendname = "John";
    $bananaprice = 2233;
    $pricelist = "The banana costs \\\$$bananaprice per a ton";
    
    $message = "Dear $friendname \n";
    $message .= "Below is the pricing you requested \n";
    $message .= $pricelist;
    
    $template = file_get_contents("emailtemplate.html");
    $template = preg_replace("^CONTENT^", $message, $template);
    
    mail("john@john.com", "banana price", $template);
    PHP:
    That extra \ in the $pricelist, which will then be in the $message, will help to escape the $ when it's thrown into preg_replace
     
    zerxer, Feb 21, 2008 IP
  10. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #10
    str_replace helped :)

    Thanks !
     
    stats, Feb 22, 2008 IP