Login

Ask Sawal Facebook Login
OR
OR

By logging in, you are agreeing to our privacy policy and terms of services.

#Question# How can we trim last character from a string using PHP?

Asked:

Hi. I want to remove last character from a string using PHP. Please suggest best solution. Thanks. 







Upload Image





To submit answer, Please login!


Answers

Total Number of Answers Available: 2


  • Profession: Blogging
    PHP Javascript Jquery C++ C#


##


Answered

Hey Hi. 

PHP rtrim function is suitable to trim last string if you are well aware about the last character of the string. Otherwise it wont be helpful. 

However, if your requirement is to remove the last string only then PHP substr function is useful. 

<?php
$str='orangem';
echo substr($str,-1);
?>
output: orange

hope it solves your problem. Thanks.


Total Good Ratings:13
Good Answer
Poor Answer

  • Profession: Editor, Writer, Blogger, SEO Expert
    seo digital marketing video editing screenwriting blogging


##


Answered

We can trim last character from a string using PHP by using php function rtrim.

Example: Bananan is the string and I want to remove the last n and want Banana string. 

Solution:

<?php
$str="Bananan";
$newstr=rtrim($str, "n");
echo $newstr;
?>
Output: Banana

Hope. This has solved your problem. Thanks. 


Total Good Ratings:46
Good Answer
Poor Answer