nadal zwraca pusty wynik, tablica $userzy
nie jest ładowana do funkcji.
Aaa, czyli chcesz wyciągnąć id
usera ze stringa, i podmienić go odpowiednim indeksem z tablicy $users
?
function replaceUsers(string $subject, array $users): string {
return preg_replace_callback('/{user:([0-9]+)}/i', function (array $match) use ($users): string {
$fullMatch = $match[0];
$id = $match[1];
if (\array_key_exists($id, $users)) {
return $users[$id];
}
throw new Exception("User '$fullMatch' not found");
}, $subject);
}
$users = [
103 => 'Mark',
123 => 'John',
];
echo replaceUsers("Mój string składa się z {user:103} oraz {UsEr:123}", $users);
Próbowałem z global
ale też bez efektu.
Zapomnij że istnieje w ogóle coś takiego jak global
.