-1

i'm creating a PHP Mailing List System. what i need is regexing some character for the name, email, etc. For Example


Hi, [[FirstName]]

blablabla

Regards, Jeff


And the regex should detect the [[]], the FirstName and replace it with some string from DB.

I'm not regex expert, i've tried some regex but can't make the perfect solution..

Thanks..

Jeho
  • 3
  • 2

2 Answers2

1

You can easily use str_replace.

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Source: PHP: str_replace

Your code could look like this (if $text contains the mail-text):

$text = str_replace("[[FirstName]]", "Name 1", $text);

If you want to replace multiple strings at once you can use arrays for $search and $replace

$text = str_replace(array("[[FirstName]]", "[[LastName]]"), array("Name 1", "Name 2"), $text);
modsfabio
  • 1,097
  • 1
  • 13
  • 29
  • Oh yeah, you're right... my mind stuck on regex. I forgot regex is for non-fixed string. Thank you so much.. – Jeho Jun 21 '17 at 06:49
0

You dont need Regex for what you want, if you have fixed variables. All you need is str_replace(). Put the Variables of your Template in the first Array and the Values from the Data-Source in the second Array().

Example:

$message = str_replace(array('{name}', '{whatever}'), array($name, $whatever), $message);
Bernhard
  • 1,852
  • 11
  • 19