- array_push: documentation ( source)
- preg_replace_callback: documentation ( source)
- print_r: documentation ( source)
- mb_strtoupper: documentation ( source)
<?php
$str = '[[Яша|ya.ru]] qwerty [[Яков|yandex.ru]]';
$dict = ['Яша' => 'Яков', 'Яков' => 'Яша'];
$res = [];
echo preg_replace_callback(
'~\[\[(.*?)\|(.*?)]]~',
function ($m) use ($dict, &$res) {
array_push($res, $m[1]);
return '<a href="'.
mb_strtoupper($m[2]) .
'" target="_blank">'.
(isset($dict[$m[1]]) ? $dict[$m[1]] : $m[1]) .
'</a>';
},
$str
) . "\n";
print_r($res);