5

How can I remove the last character dynamically from a PHP string variable?

$string = '10,20,30,';
echo $string;

Available output:

10,20,30,

Required output:

10,20,30
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
  • 2
    echo rtrim($string,','); – Hanky Panky Nov 27 '16 at 10:07
  • `echo substr($string, 0, strlen($string)-1);` – connexo Nov 27 '16 at 10:09
  • This is one of the top hits for `site:stackoverflow.com end of character PHP`. [The duplicate](https://stackoverflow.com/questions/2053830/how-do-i-remove-all-specific-characters-at-the-end-of-a-string-in-php) also has other methods, incl. using regular expressions, `mb_substr`, and `trim`. – Peter Mortensen Jan 02 '23 at 16:21
  • [An answer](https://stackoverflow.com/questions/4915753/how-can-i-remove-three-characters-at-the-end-of-a-string-in-php/4915787#4915787) covers ***unconditionally*** removing the last character (that ***this*** question is if taken literally as written): `substr($string, 0, -1);` – Peter Mortensen Jan 02 '23 at 16:26

5 Answers5

7

rtrim($string, ","); removes a comma at the end of the string.

PHP.net rtrim

trim($string, ","); removes a comma at the beginning and end of a string.

PHP.net trim

You can also use implode if you're working with arrays:

PHP.net implode

Example from php.net:

<?php
    $array = array('lastname', 'email', 'phone');
    $comma_separated = implode(",", $array);

    echo $comma_separated; // lastname,email,phone

    // Empty string when using an empty array:
    var_dump(implode('hello', array())); // string(0) ""
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tchartron
  • 669
  • 10
  • 18
  • Right! C'mon you just copied my answer. No hard feelings though. It is fun to see people grasp for points. Go for it! – RST Nov 27 '16 at 10:17
  • No, I thought about implode cause the string looks a lot like it's coming from an array – tchartron Nov 27 '16 at 10:20
  • 1
    @RST: What do you mean? You posted your answer 100 seconds later. Did something happen in the 5 minute edit grace period (in the remaining 200 seconds (3 min 20 secs))? – Peter Mortensen Jan 02 '23 at 15:54
3

echo substr($string, 0, strlen($string)-1);

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
connexo
  • 53,704
  • 14
  • 91
  • 128
  • 1
    why 2 functions when it can be done with 1? Is it faster? – RST Nov 27 '16 at 10:12
  • 1
    What if you don't know the character to delete? – connexo Nov 27 '16 at 10:12
  • Seems like he does :) – RST Nov 27 '16 at 10:12
  • 1
    I prefer methods with generic applicability. – connexo Nov 27 '16 at 10:14
  • 1
    This is a better solution @RST no the user did not mention that the last character will always be a comma – Qarib Haider Nov 27 '16 at 10:20
  • This is a right answer (though somewhat terse). The first two are too closely tied to the specific example string (the text in the question does not say anything about it only being a comma). – Peter Mortensen Jan 02 '23 at 15:59
  • [`strlen` is claimed to be unnecessary](https://stackoverflow.com/questions/4915753/how-can-i-remove-three-characters-at-the-end-of-a-string-in-php#comment32990960_4915775) (PHP version dependent?). [Example](https://stackoverflow.com/questions/4915753/how-can-i-remove-three-characters-at-the-end-of-a-string-in-php/4915779#4915779). – Peter Mortensen Jan 02 '23 at 16:24
0

You could use rtrim():

rtrim($string, ',');

But I think your problem lies elsewhere. It seems like you are adding options to this string in a for/while/foreach loop.

Use this instead:

$string = array[];
foreach ($parts as $part) {
  $string[] = $part;
}
$string = implode($string, ',');
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RST
  • 3,899
  • 2
  • 20
  • 33
0

Yes! I have gotten an answer:

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

echo $string;

Output

10,20,30

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
0

If PHP > 7.1 then

$string[-1] = PHP_EOL;

DEMO

nektobit
  • 843
  • 7
  • 11
  • This may work, but can you add something to substantiate the claim, e.g. one or more references? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://https://stackoverflow.com/posts/52601696/edit), not here in comments (***************************************** ***without*** ***************************************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 02 '23 at 16:06