PHP array_pop & implode show question mark when used with foreign language

126 Views Asked by At

I'm using the following code to display only the last name first character on my Wordpress site:

$name = $comment->comment_author;
$separate = mb_split(" ", $name);
$last = array_pop($separate);
echo implode(' ', $separate)." ".$last[0].".";

It works great with English names, but $last[0] will return a question mark when used with a foreign language (e.g Arabic, Hebrew, Greek etc). For example:

Name: השם שלי

Will return:

השם ?.

I've been trying to fix this for an hour now, but nothing so far.

Any idea?

1

There are 1 best solutions below

0
Radu On

You can try to set the encoding (https://www.php.net/manual/ro/function.mb-internal-encoding.php) before using the mb_split function.