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?
substr_replace() replaces a copy of string delimited by the offset and (optionally) length parameters with the string given in replace .
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.
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.
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.
if (substr($str, -1) == 's')
{
$str = substr($str, 0, -1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With