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
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.
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
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 \).
the emailtemplate.html is a simple HTML template something like <header code goes here with styles and everything> CONTENT <footer code goes here>
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.
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:
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