ktm
ktm

Reputation: 6085

how to parse php variable with html?

Hi friends I have created newsletter system. In which I want to send person name like typing Hello %username% in html form, which will be coming from database $username. So when the person get email He will have "Hello Falana".

Thanks

Upvotes: 0

Views: 327

Answers (3)

Veign
Veign

Reputation: 606

If you have multiple find/replaces look into the preg_replace() function.

$data = 'Find1 / Find2 / Find3';

$patterns[0] = '/Find1/';
$patterns[1] = '/Find2/';
$patterns[2] = '/Find3/';

$replacements[0] = 'Replace1';
$replacements[1] = 'Replace2';
$replacements[2] = 'Replace3';

$data = preg_replace($patterns, $replacements, $data);

Upvotes: 0

Tokk
Tokk

Reputation: 4502

you can go with str_replace:

$html = "Hello, %username% .....";
$html = str_replace("%username%", $username, $html);

where $username contains the real name from the db.

Upvotes: 1

Stewie
Stewie

Reputation: 3131

The concept is called mail merge. You will have to write a script to do that. This is how it will work: (its for you to get a rough idea, NOT a code)

$emailBody = " Hello %USERNAME%"; 


foreach($members as $member_information){
$emailBody = str_replace('%USERNAME%',$member_information['user_name'],$emailBody);

sendMail($member_information['email_address'],$emailBody);
} 

Upvotes: 3

Related Questions