98

How do I remove the last character only if it's a period?

$string = "something here.";
$output = 'something here';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ggggggggg
  • 1,179
  • 1
  • 8
  • 9

9 Answers9

174
$output = rtrim($string, '.');

(Reference: rtrim on PHP.net)

Mike Kormendy
  • 3,389
  • 2
  • 24
  • 21
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 2
    It might be unwanted to remove ... (elipsis): – mys Oct 12 '12 at 01:45
  • 2
    @OndrejIvanic: You have `…` for that. If you remove the last dot, and the remaining is still a dot, the question title wouldn't make much sense, would it? – Alix Axel Oct 12 '12 at 13:20
35

Using rtrim() replaces all "." at the end, not just the last character:

$string = "something here..";
echo preg_replace("/\.$/", "", $string);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • 3
    I'm guessing the OP wants to remove all '.' at the end, otherwise why remove just one? ;) – Tor Valamo Jan 13 '10 at 02:05
  • 2
    yes, i am guessing that too. just following the question as close as possible. – ghostdog74 Jan 13 '10 at 02:10
  • 2
    This is much better, too many people don't realize using `character_mask` in `trim` basically removes any instance of the character in the entire string – Brock Hensley Jun 19 '14 at 18:51
  • This will remove the dots too if the string ends with a newline sequence: `$string = "something here..\n";` because `$` matches at the position of this last newline too. To avoid that, add the D (Dollar End Only) modifier or change `$` to `\z` (z is lowercase). – Casimir et Hippolyte Jun 20 '23 at 22:38
8

To remove the last character only if it's a period and not resorting to preg_replace, we can just treat the string as an array of characters and remove the final character if it is a dot.

if ($str[strlen($str) - 1] === '.')
  $str = substr($str, 0, -1);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
C.O.
  • 2,281
  • 6
  • 28
  • 51
  • 2
    This is the correct answer since the other ones would delete all trailing characters not just the last one. But you should use `===` instead of `==` to also check for type equality – padawanTony Jul 07 '16 at 09:43
2

Use:

$string = "something here..........";

ltrim would remove leading dots. For example: ltrim($string, ".")

rtrim rtrim($string, ".") would remove trailing dots.

trim trim($string, ".") would remove trailing and leading dots.

You can also do this by regular expressions.

preg_replace would remove. It can be used to remove dot/dots at the end:

$regex = "/\.$/"; // To replace single dot at the end
$regex = "/\.+$/"; // To replace multiple dots at the end
preg_replace($regex, "", $string);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shahbaz
  • 3,433
  • 1
  • 26
  • 43
2

The last character can be removed in different ways. Here are some:

  • rtrim()

    $output = rtrim($string, '.');
    
  • Regular Expression

    preg_replace("/\.$/", "", $string);
    
  • substr() / mb_substr()

    echo mb_substr($string, 0, -1);
    
    echo substr(trim($string), 0, -1);
    
  • substr() with trim()

    echo substr(trim($string), 0, -1);
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nishad Up
  • 3,457
  • 1
  • 28
  • 32
1

You can use the rtrim() function of PHP which allows you to trim the data which exists in the last position.

For example:

$trim_variable = rtrim($any_string, '.');

It is the simplest and fastest way!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

The chop() function removes whitespaces or other predefined characters from the right end of a string

$string = "something here.";

$string = chop($string,".");

echo $string;

Output

something here

Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
  • [chop() is an alias of rtrim()](https://www.php.net/manual/en/function.chop.php), and thus it is effectively covered by the previous answers. – Peter Mortensen Jan 03 '23 at 22:03
0

Use a combination of strrpos and substr to get the position of the last period character and remove it, leaving all other characters intact:

$string = "something here.";

$pos = strrpos($string, '.');
if($pos !== false) {
  $output = substr($string, 0, $pos);
} else {
  $output = $string;
}

var_dump($output);

// $output = 'something here';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GreensterRox
  • 6,432
  • 2
  • 27
  • 30
-1

Example:

<?php

    $columns = array('col1' => 'value1', 'col2' => '2', 'col3' => '3', 'col4' => 'value4');

    echo "Total no of elements: " . count($columns);
    echo "<br>";
    echo "----------------------------------------------<br />";

    $keys = "";
    $values = "";
    foreach($columns as $x => $x_value)
    {
      echo "Key=" . $x . ", Value=" . $x_value;
      $keys = $keys . "'" . $x . "',";
      $values = $values . "'" . $x_value . "',";
      echo "<br>";
    }


    echo "----------------------Before------------------------<br />";

    echo $keys;
    echo "<br />";
    echo $values;
    echo "<br />";

    $keys   = rtrim($keys, ",");
    $values = rtrim($values, ",");
    echo "<br />";

    echo "-----------------------After-----------------------<br />";
    echo $keys;
    echo "<br />";
    echo $values;

?>

Output:

Total no of elements: 4
----------------------------------------------
Key=col1, Value=value1
Key=col2, Value=2
Key=col3, Value=3
Key=col4, Value=value4
----------------------Before------------------------
'col1','col2','col3','col4',
'value1','2','3','value4',

-----------------------After-----------------------
'col1','col2','col3','col4'
'value1','2','3','value4'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131