Reputation: 3126
I have this block of text which should be customizable in that some of the words/keywords that can be customized. Let's say this is the block of text below.
Dear [Name], Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat on [Date]. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.Please contact [PhoneNumber]
The words within square brackets are the keywords that should be replaceable. The data to replace them will come from db, which is fine. What I want to know is that what is the best way to do this. Should I just search for particular keywords one by one (Many more keywords are there, but there's no guarantee which one may feature in which block of text, so I will have to check for all possible keywords for every block of text) and then replace them with the appropriate value using str_replace
? Or, is there a better way do go about it? Thanks.
Upvotes: 4
Views: 251
Reputation: 1
$admin_email_text = 'This is [first-field-label] the test, you can send the email at [form-email]';
$admin_email_text = str_replace("[form-email]", $biz_field_email, $admin_email_text);
$admin_email_text = str_replace("[first-field-label]", $biz_field_one, $admin_email_text);
print $admin_email_text;
Upvotes: 0
Reputation: 13725
str_replace can replace a whole array in one step:
$map = array('[PhoneNumber]'=>'...', '[Date]'=>'...',...);
$result = str_replace(array_keys($map), array_values($map), $input);
Upvotes: 5