, , - , :
$words=array("_saudation_"=>"Hello", "_animal_"=>"cat", "_animal_sound_"=>"MEooow");
$source=" _saudation_! My Animal is a _animal_ and it says _animal_sound_ , _no_match_";
echo (preg_replace_callback("/\b_(\w*)_\b/", function($match) use ($words) { if(isset($words[$match[0]])){
return ($words[$match[0]]);}else{ return($match[0]);}}, $source));
Returns: Hello! My Animal is a cat, and she says that MEooow, _no_match _
Please note that although "_no_match_" does not have a translation, it will match during regular expression but will retain its key.
source
share