php - Eval() only way i know, is there other? -


i have language files translations of content in website.
each different language load different file.

now have this, need echo on page , include in email sent on form submit:

$lang["booking_mail_header"]='dear '.$first_name.', thank contact!'; 

the problems here $lang[] loaded before $first_name defined , $first_name user inputed content, unsafe.

i thought using eval() , thought again , didn't. there way?

the variable in point of script has passed regex check:
preg_match('/^[a-öa-Ö0-9-. ]*$/', $first_name)

you make template, e.g.

$lang["booking_mail_header"]='dear %s, thank contact!'; 

then use sprintf

$msg=sprintf($lang["booking_mail_header"], $first_name); 

you cleverer, , use numbered arguments, allow templates reorder inputs sprintf, e.g.

  $lang["format_name"]='%2$s, %1$s';    $msg=sprintf($lang["format_name"], $first_name, $last_name); 

once you've understood that, consider using gettext, uses same basic idea, wraps in standard tools, can aid keeping translations maintained. using that, wouldn't keep translations in array, you'd wrap natural language in _() wrapper, e.g.

$msg=sprintf(_('dear %1$s, thank contact!'), $first_name); 

gettext tools can extract natural language wrapped _() function separate file translation. translator use standard tool poedit produce these translations. @ runtime, select appropriate translated version of file, , _() return translation.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -