Get first name with Regular Expression

5

I need to get the first name with regular expression, but I'm not getting it right. Today I use the code like this:

<?php
preg_match('/[[:alnum:]]+/i', 'Alisson Acioli', $matches);

return $matches[0];
?>
  

Output: Alisson

When the name is composed:

<?php
preg_match('/[[:alnum:]]+/i', 'João', $matches);

return $matches[0];
?>
  

Output: Jo

    
asked by anonymous 29.12.2015 / 18:36

5 answers

0

Try using this code:

<?php
    preg_match('/[^\s]*/', 'Qualquer Nome', $matches);
    return $matches[0];
?>
    
29.12.2015 / 18:44
5

You asked for a regular expression, but I'll leave some alternatives with string operation, in case someone prefers without RegEx (actually RegEx is not for these simple things).

Explode

$tokens = explode( 'Alisson Acioli', $nome );
return $tokens[0];

with verification 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 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 you use encodings of 1 byte only, you can remove the prefixes.

    
29.12.2015 / 18:42
2

Another way to resolve with regex is:

$str = 'joão da silva';

preg_match('/\[a-z]+/ui', $str, $m);

The PCRE modifier u is important in this case to make the capture of accented characters from the opposite will capture only singlebyte characters as in the question example. The modifier i makes the capture caseinsensitive, that is to say whether or not the letters are case-sensitive.

Other option to solve this problem see: Printing a String until a space is found

    
29.12.2015 / 18:45
2

You can test with \p{L}+ , which looks for characters with specific characteristics .

In this case the code looks like this:

preg_match('/\p{L}+/i', 'Alisson Acioli', $matches);
return $matches[0];
    
29.12.2015 / 18:46
2

This is due to the accentuation of the word joão , because [[:alnum:]] does not consider accentuation. Consider using:

preg_match('/[\p{Latin}\d]+/i', 'Joaõ da Silva', $matches);
echo $matches[0];
    
29.12.2015 / 18:49