2

I have looked and looked for an example on this, but the only ones I have found are echoing the result of the str replace like this :

$malestr = str_replace("{name}", "$name", $rawstring);
echo "$malestr";

But this is not what I want, what I am trying to do is....... when I create a template, in the text box field for the message I would write :

Dear {name}, Thankyou for your order !.

Name being the person I am writing too, that is pulled from the $toPersonsName array when the email is sent :

So the result of this would be in the email as follows :

If the customer was called bob, it would be >>

Dear Bob, Thankyou for your order !.

Is this str_replace I need for this or is it something else, as I don't want to have to write

Dear <?php echo '$malestr';?>

every time I create a template, {name} would be better.

EDIT >>

If I do the following :

$name = 'BOB';
$rawstring = "$name";

$malestr = str_replace("{name}", "$name", $rawstring);


echo "$malestr";
echo "{name}";

It echos {name} , but not bob ??, yet $malestr echo's bob, how can I get {name} to echo bob ?

Iain Simpson
  • 441
  • 4
  • 13
  • 29
  • What is wrong with the code you already have? What is the issue with using the value stored in the variable? – Michael Mior Jan 03 '12 at 11:36
  • if I echo 'Dear {name}, Thankyou for your order!.' I just get {name}, it doesnt replace it with the name – Iain Simpson Jan 03 '12 at 11:40
  • possible duplicate of [Substring replacement using str_replace](http://stackoverflow.com/questions/8710376/substring-replacement-using-str-replace) – nikc.org Jan 03 '12 at 12:19
  • @IainSimpson don't ask the same question again even if you don't get an answer. Instead, improve your original question. See http://stackoverflow.com/faq#bounty – nikc.org Jan 03 '12 at 12:20

2 Answers2

0

There are various methods of string replacement and you can see in this blog post how they compare as far as speed.

Ultimately you need to parse your entire view output (if you are using MVC) and replace each instance. The popular template engine, Smarty, uses something very similar to what you are trying to achieve. It is open source if you would like to look at how they accomplish this function.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
0

You can use strtr() native function to do this.