How to get the last word of a string in PHP?

-1

For example: "JOHN FULANO DE TAL". I just want to get the word "TAL".

    
asked by anonymous 08.08.2018 / 00:40

4 answers

5

Using the strrchr() function view the ideone

$string ="Eu não recomendaria o uso de expressões regulares, pois é desnecessário, a menos que você realmente queira, por algum motivo";

$ultima_palavra = strrchr($string,' ');

echo $ultima_palavra;

strrchr () - returns the part that starts in the last instance

Another way:

$string = 'JOÃO FULANO DE TAL';
$partes = explode(' ', $string);
$ultima_palavra = array_pop($partes);

echo $ultima_palavra;

explode() separates a string into an array of several smaller strings based on a divider character, which can be a period, a comma, or any other character or string (in this case a space). Its syntax looks like this:

explode( separador,string,limite )
  • separador : The character that must be found inside the string to divide it;
  • string : The text we want to divide into;
  • limite : This is the number of times the command should split the string. This parameter is optional and if not informed the division will be all string.

    array_pop() extracts and returns the last element of the array

08.08.2018 / 01:28
6

The simplest and a little naive would be this (I do not guarantee that it meets any situation, but the question does not say much:

$palavras = explode(' ', 'José da Silva');
echo $palavras[count($palavras) - 1];

You use a function that already breaks space-based words by generating an array , then just get the last element of it.

According to the bfavaretto below you can do a little better and so in a slightly less naive way:

$texto = 'José da Silva';
$texto = trim($texto);
echo substr($texto, strrpos($texto, ' ') + 1);

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

08.08.2018 / 00:53
1

You can use preg_match () with a regular expression:

preg_match("|\w+?$|", $string, $ultima);

You will always get the last 1 characters from the end of the text behind ( $ ) to alphanumeric or < in> underscore ( \w ), in this case, space before "TAL".

<?
$string = "JOÃO FULANO DE TAL";
preg_match("|\w+?$|", $string, $ultima);
echo $ultima[0]; // imprime: TAL
?>
  

1 Only works with words without special characters.

    
08.08.2018 / 01:32
1

Supplemented the answers already given; there are some means to do, none properly "right." What can be done is to isolate and leave as much of the code as possible.

The first example would be to use RegEx:

$string = "mauro é perfeito";

preg_match("/[\w\-]+$/", $string, $matches);

echo $matches[0]; //perfeito

But since nothing is perfect (nor me), using regex is not the simplest way, but in relation to explode() it is "in front" in XGH .

The second path, using explode() would use the end() method to get the last occurrence.

$string = "mauro é perfeito";
$string = explode(" ", $string );

$last_word = end($string);

echo $last_word; //perfeito
    
08.08.2018 / 01:34