Skip to content

Instantly share code, notes, and snippets.

@lenivene
Created February 18, 2018 17:40
Show Gist options
  • Save lenivene/1cab32212acac9de695aad5c35cace32 to your computer and use it in GitHub Desktop.
Save lenivene/1cab32212acac9de695aad5c35cace32 to your computer and use it in GitHub Desktop.
Get initial letters
<?php
function initial_letters( $char ){
// Defaults
$pos = 0;
$saida = '';
if( !isset( $char ) ){
trigger_error( "Insert text", E_USER_ERROR );
return false;
}
while( ( $pos = mb_strpos( $char, ' ', $pos ) ) !== false ){
if( isset( $char[$pos + 1] ) && $char[ $pos + 1 ] != ' ' ){
$saida .= mb_substr( $char, $pos + 1, 1 );
}
$pos++;
}
return $char[0] . $saida;
}
// Example:
$char = "Lenivene Bezerra";
print initial_letters( $char ); // Return LB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment