I am not a php dev and would like some help to understand
$keys = array_keys($parameters['macrons']);
$pattern = '/([' . implode('', $keys) . '])ー/u';
return preg_replace_callback($pattern, function($matches) use ($parameters) {
return $parameters['macrons'][$matches[1]];
}, $str);
There is also a yaml file looking like this:
- function: transliterateChoonpu
parameters:
macrons:
a: ā
i: ī
u: ū
e: ē
o: ō
This code look for some vowels followed by the ー(U+30FC Unicode character) in the string $str
and replace them with their equivalent chōonpu (seems obvious regarding the function name).
$keys = array_keys($parameters['macrons']);
$parameters['macrons']
is an associative array:
array(
'a' => 'ā',
'i' => 'ī',
'u' => 'ū',
'e' => 'ē',
'o' => 'ō'
)
The keys are referenced by $keys
: ['a', 'i', 'u', 'e', 'o']
.
$pattern = '/([' . implode('', $keys) . '])ー/u';
Imploding the keys without separator allow to make the resulting pattern '/([aiueo])ー/u'
which matches any vowel (except y) followed by the U+30FC Unicode character (called Katakana-Hiragana Prolonged Sound Mark) in a Unicode string.
return preg_replace_callback($pattern, function($matches) use ($parameters) {
return $parameters['macrons'][$matches[1]];
}, $str);
For each matching vowel, preg_replace_callback calls the anonymous function passed as the second parameter giving it the matched vowel. The result of this callback replaces the matched vowel in the resulting string (returned by preg_replace_callback).
The callback simply returns the value indexed by the matched vowel in the $parameters['macrons']
associative array.
So, the vowels are replaced by their corresponding chōonpu !