You asked with regular expression, but I’ll leave some alternatives with operation string, if someone prefers without Regex (in fact, Regex is not for these simple things).
Explodes
$tokens = explode( 'Alisson Acioli', $nome );
return $tokens[0];
with verification if found:
$nome = 'Alisson Acioli';
$tokens = explode( ' ', $nome );
return count( $tokens ) > 0 ? $tokens[0] : '';
Substr + strpos
$nome = 'Alisson Acioli';
return mb_substr( $nome, 0, mb_strpos( $nome, ' ' ) );
with verification if found:
$pos = mb_strpos( $nome, ' ' );
return $pos !== false ? mb_substr( $nome, 0, $pos ) : '';
strstr
$nome = 'Alisson Acioli';
return mb_strstr( $nome, ' ', true );
In all cases, the prefix mb_
is for strings multibyte. If used encodings 1 byte only, can remove prefixes.
Use the modifier
u
to capture multibyte characters.strstr()
already fix it => Printing a String until a space is found– rray
And why you need to pick with regular expression?
– Bacco
really want to do this with ER?
– Daniel Omine
Coincidence, I just saw a similar question: http://answall.com/questions/105977/com-esta-regra-abaixo-como-fa%C3%A7o-para-mostrar-o-primeiro-e-o-%C3%Baltimo-nome-do-Usu%C3%A1
– Daniel Omine