php - how to replace text with another similar to mail merge? -


i working in magento, , have developed module send text messages customers. in settings of module, admin can set message sent customer. i'm trying add features allow replacement of texts data database.

for example, have following code fetches saved settings body of text message:

$body = $settings['sms_notification_message']; 

the message fetched looks this:

dear {{firstname}},  order ({{ordernumber}}) has been shipped. tracking#: {(trackingnumber}}  business! {{storename}} 

the goal have module replace variables in "{{ }}" customer , store information. unfortunately, i'm unable figure out how make replace information before sending message. being send is.

the easiest way use str_replace, so:

// set message $message = <<< message dear {{firstname}},  order ({{ordernumber}}) has been shipped. tracking#: {{trackingnumber}}  business! {{storename}}  message;  // assign values in associative array $values = [     'firstname' => 'firstnamevalue',     'ordernumber' => 'ordernumbervalue',     'trackingnumber' => 'trackingnumbervalue',     'storename' => 'storenamevalue' ];  // create arrays $target indicating value change $targets = []; foreach ($values $k => $v) {     $targets[] = '{{'.$k.'}}'; }  // use str_replace perform substitution echo str_replace($targets,$values,$message); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -