I had an issue with multibyte character encoding, my implementation below:
```php
function mb_rawurlencode(string $url): string
{
return implode('', array_map(
static fn (string $char): string => preg_match('/[\w-]/', $char)
? $char
: sprintf('%%%s', wordwrap(strtoupper(bin2hex($char)), 2, '%', true)),
mb_str_split($url)
));
}
```