Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the Last "s" with "" in PHP

I need to know how I can replace the last "s" from a string with ""

Let's say I have a string like testers and the output should be tester. It should just replace the last "s" and not every "s" in a string how can I do that in PHP?

like image 217
streetparade Avatar asked Mar 28 '10 21:03

streetparade


People also ask

How can I change the last character of a string in PHP?

substr_replace() replaces a copy of string delimited by the offset and (optionally) length parameters with the string given in replace .

How can I replace an accented character with a normal character in PHP?

php $transliterator = Transliterator::createFromRules(':: NFD; :: [:Nonspacing Mark:] Remove; :: NFC;', Transliterator::FORWARD); $test = ['abcd', 'èe', '€', 'àòùìéëü', 'àòùìéëü', 'tiësto']; foreach($test as $e) { $normalized = $transliterator->transliterate($e); echo $e.

How can I remove last 4 characters from a string in PHP?

Method 4 – rtrim function The rtrim function to used to remove specific characters from the end of the string. rtrim($string,'x'); Here “x” is the character to remove.

What is substr () in PHP and how it is used?

substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above.


1 Answers

if (substr($str, -1) == 's')
{
    $str = substr($str, 0, -1);
}
like image 143
zerkms Avatar answered Sep 21 '22 07:09

zerkms