ggggggggg
ggggggggg

Reputation: 1189

How do I remove all specific characters at the end of a string in PHP?

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

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

Upvotes: 99

Views: 58348

Answers (9)

Chittaranjan Sethi
Chittaranjan Sethi

Reputation: 442

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'

Upvotes: -1

Shahbaz
Shahbaz

Reputation: 3463

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);

Upvotes: 2

Nishad Up
Nishad Up

Reputation: 3615

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);
    

Upvotes: 2

Moiz Shafqat Husain
Moiz Shafqat Husain

Reputation: 219

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!

Upvotes: 1

GreensterRox
GreensterRox

Reputation: 7190

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';

Upvotes: 0

C.O.
C.O.

Reputation: 2329

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);

Upvotes: 9

ghostdog74
ghostdog74

Reputation: 343067

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

$string = "something here..";
echo preg_replace("/\.$/", "", $string);

Upvotes: 35

Majbah Habib
Majbah Habib

Reputation: 8558

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

Upvotes: 1

Alix Axel
Alix Axel

Reputation: 154673

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

(Reference: rtrim on PHP.net)

Upvotes: 175

Related Questions