Для замены множественного и полного массива ассоциативным ключом вы можете использовать это, чтобы соответствовать вашему шаблону регулярных выражений:
$words=array("_saudation_"=>"Hello", "_animal_"=>"cat", "_animal_sound_"=>"MEooow");
$source=" _saudation_! My Animal is a _animal_ and it says _animal_sound_ ... _animal_sound_ , _no_match_";
function translate_arrays($source,$words){
return (preg_replace_callback("/\b_(\w*)_\b/u", function($match) use ($words) { if(isset($words[$match[0]])){ return ($words[$match[0]]); }else{ return($match[0]); } }, $source));
}
echo translate_arrays($source,$words);
//returns: Hello! My Animal is a cat and it says MEooow ... MEooow , _no_match_
* Обратите внимание, что хотя "_no_match_" не переведено, оно будет соответствовать во время регулярного выражения, но
сохранить свой ключ. И ключи могут повторяться много раз.