Skip to content

Instantly share code, notes, and snippets.

@anggadarkprince
Last active November 30, 2016 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anggadarkprince/e8e4baf15ee9c2507f20c5b28c3f28a4 to your computer and use it in GitHub Desktop.
Save anggadarkprince/e8e4baf15ee9c2507f20c5b28c3f28a4 to your computer and use it in GitHub Desktop.
Dynamic Function
<?php
$words = ['dibawakan', "dimakan", "menangis"];
$list = ['bawa', 'makan'];
$complete = [];
$functions = ['trim_prefix', 'trim_suffix', 'trim_another_affix'];
function trim_prefix($word){
if(preg_match('/^(di|[ks]e)/', $word)){
return preg_replace('/^(di|[ks]e)/', '', $word);
}
return $word;
}
function trim_suffix($word){
if(preg_match('/(kan)$/', $word)){
return preg_replace('/(kan)$/', '', $word);
}
return $word;
}
function trim_another_affix($word){
return $word;
}
foreach ($words as $word) {
foreach ($functions as $callable) {
$word = call_user_func($callable, $word);
if(in_array($word, $list)){
array_push($complete, $word);
break;
}
}
}
print_r($complete);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment