views:

285

answers:

6

Hi, 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?

+10  A: 
if (substr($str, -1) == 's')
{
    $str = substr($str, 0, -1);
}
zerkms
Removes only the `s` if it is the last character.
Felix Kling
orly? may be becaue streetparade needs in this "Hi, I need to know how I can replace the last "s" from a string with """ ? ;-)))
zerkms
How can I remove the the last `s` in `"String with s not in last position"`?
Felix Kling
You'd need to break down the string into words first to make this work.
Veger
@Felix: you can remove with any solution you want, but streetparade needs only in removing "s" when it's actually last char in the string.
zerkms
@zerkms: Where does he states this?
Felix Kling
your answer is useful because of elegant preg. sorry for -1 vote. changed back :-(
zerkms
@zerkms: Thank you. Deleted my comment anyway as it is not really helping the discussion ;)
Felix Kling
+3  A: 

Your question is somewhat unclear whether you want to remove the s from the end of the string or the last occurence of s in the string. It's a difference. If you want the first, use the solution offered by zerkms.

This function removes the last occurence of $char from $string, regardless of it's position in the string or returns the whole string, when $char does not occur in the string.

function removeLastOccurenceOfChar($char, $string)
{
    if( ($pos = strrpos($string, $char)) !== FALSE) {
        return substr_replace($string, '', $pos, 1);
    }
    return $string;
}
echo removeLastOccurenceOfChar('s', "the world's greatest");
// gives "the world's greatet"

If your intention is to inflect, e.g singularize/pluralize words, then have a look at this simple inflector class to know which route to take.

Gordon
nice sample, that demonstrates nothing ;-) function doanything($char, $string) { return $string; } will work in the same manner but do this faster
zerkms
ah, i missed that "s" from "greatest" disappeared. i need glasses. anyway - this doesn't replaces "s" from the string only if "s" is at the last position
zerkms
@zerkms I still dont get what you mean. The function replaces exactly the last *occurence* of the $char, regardless of where it is in the string. The OP wasn't clear about whether he wanted to remove the 's' from the *end* or the *last occurence*, which is a difference.
Gordon
+1  A: 
$result = rtrim($str, 's');
$result = str_pad($result, strlen($str) - 1, 's');

See rtrim()

Dor
Nope. Huntress => Huntre
Gordon
Correct, I fixed my answer.
Dor
Supreme overkill in comparison to zerkms' solution :)
David Caunt
@David Caunt: indeed :)
Dor
+7  A: 

Update: Ok it is also possible without regular expressions using strrpos ans substr_replace:

$str = "A sentence with 'Testers' in it";
echo substr_replace($str,'', strrpos($str, 's'), 1);
// Ouputs: A sentence with 'Tester' in it

strrpos returns the index of the last occurrence of a string and substr_replace replaces a string starting from a certain position.

(Which is the same as Gordon proposed as I just noticed.)


All answers so far remove the last character of a word. However if you really want to replace the last occurrence of a character, you can use preg_replace with a negative lookahead:

$s = "A sentence with 'Testers' in it";
echo preg_replace("%s(?!.*s.*)%", "", $string );

// Ouputs: A sentence with 'Tester' in it
Felix Kling
dude, all he need is to replace 1 char from the string
zerkms
lol. +1 to both of you. What an awesome answer. @Felix, if I asked you how to add int's in C++, would you start with "Well, first you create a boolean `carry` variable to store the carry after the addition of each bit."? :P
Cam
@zerkms: At least it is exactly what he asks for.
Felix Kling
@incrediman: No of course not ;) But in my opinion it is better to start with a "not optimal" solution that produces the desired results and go on from there as with a fast solution that does not work in all cases.
Felix Kling
@Felix: Yeah - well it's an interesting regex anyways :)
Cam
if you decide to solve your problem with regex - you get 2nd issue (can't find the original) ;-)
zerkms
A: 

True i was looking for this solution long back

Deb
Is this supposed to be an answer? This is not a forum. Only answer if you provide some solution.
Felix Kling
A: 
$str = preg_replace("/s$/i","",rtrim($str));
Leo