Remove last space from a variable

Asked

Viewed 519 times

-1

For example:

$name = 'Fulano De Tal    ';

How to remove that last space, to look like this: (Fulano De Tal) ?

  • Use the function trim($name), it also removes spaces at the beginning.

4 answers

4


1) Removing the last blank

To remove the last space, you will first need to make sure that it is a blank space. If it is, return all the string except the last character.

if (mb_substr($name, -1) === ' ') {
    $name = mb_substr($name, 0, mb_strlen($name)-1);
}

Using the functions mb_* to be safe against multi-byte characters. Thus, we would have:

$name = 'Fulano De Tal    ';

 if (mb_substr($name, -1) === ' ') {
    $name = mb_substr($name, 0, mb_strlen($name)-1);
}

echo $name; // 'Fulano De Tal    '

2) Removing all whitespace from the end

However, with the example you passed, which goes against the text of the question, you want to remove all blank spaces end of text. For this, you will need the function rtrim:

$name = 'Fulano De Tal    ';

$name = rtrim($name);

echo $name;  // 'Fulano de Tal'

3) Removing all whitespace from both start and end

And finally, if you want to make sure there’s no white spaces at both the end of the text and the beginning, there yes you do as placed in the other answers, using the function trim:

$name = '     Fulano De Tal    ';

$name = trim($name);

echo $name;  // 'Fulano de Tal'

It is of utmost importance to stress that the functions trim, ltrim and rtrim, by default, will remove any characters listed below:

  • " " (ASCII 32 (0x20)), normal space.
  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13(0x0D)), a car reset.
  • "\0" (ASCII 0 (0x00)), the NULL byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tabulation.

That is, to do trim("Fulano de Tal\t\n\r\0 ") would also return "Fulano de Tal". If this is not the desired behavior, you can manually indicate which are the characters you want to remove through the second parameter of the function:

trim("Fulano de Tal\t\n\r\0    ", " ")  // "Fulano de Tal\t\n\r\0"
  • +1 - I was going to comment on this. The title mentions the last space, but the example removes all spaces from the end. And your answer was the only one who understood that...

  • @hkotsubo I realized this too, so I replied about the rtrim()

  • @Fabianomonteiro The title of the question says "remove the last space" and this gives to understand that is only one space (only the last, even if there is more), and rtrim does not solve this case. The example given in the question, however, is contradictory to the title (and so it is not possible to know what the goal really is, whether the author of the question erred in the title or in the example), and this was the only answer that noticed and mentioned this...

  • @hkotsubo truth, friend

  • Perfect, what worked was: rtrim($name); Removing all spaces at the end! Thank you very much!

0

using trim doesn’t solve?

<?php
 echo trim('     Removendo Espaços     '); //Resultado: 'Removendo Espaços'
?>

0

You can use the function trim, basically the Trim function removes existing whitespace before and after a string.

trim($name)

0

If you want to remove whitespace only from the end of a string, use the function rtrim().

'Fulano De Tal    ' //Fulano De Tal//

ithrim() removes whitespace only from the beginning of a string.

'    Fulano De Tal    ' //Fulano De Tal   //

If you want to remove blanks from both ends, then use Trim().

'    Fulano De Tal    ' //Fulano De Tal//

Remove all whitespace in a string by replacing multiple whitespace characters with a single space.

<?php

$name = '     Fulano             Fulano Fulano De Tal    Fulano    ';

$resultado = trim(preg_replace('/\s+/', ' ', $name));

print $resultado; //Fulano Fulano Fulano De Tal Fulano//

Browser other questions tagged

You are not signed in. Login or sign up in order to post.