Skip to content

Instantly share code, notes, and snippets.

@martsie
Created July 30, 2020 21:58
Show Gist options
  • Save martsie/9e4b5a79fdfc5c528829c09538a06968 to your computer and use it in GitHub Desktop.
Save martsie/9e4b5a79fdfc5c528829c09538a06968 to your computer and use it in GitHub Desktop.
Split a name into first name and last initial without UTF8 malformed errors
<?php
function convertName($full_name) {
$split_name = explode(' ', $full_name);
if (count($full_name) !== 2) {
// Single word name.
return $full_name;
}
// Must use mb_substr otherewise utf8 issues can occur with foreign names.
return mb_substr($split_name[0], 0, 1) . ' ' . mb_substr($split_name[1], 0, 1) . '.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment