Jump to content

[SOLVED] How to strip the last "s" from the word Shows?


ballhogjoni

Recommended Posts

How about the function http://us.php.net/manual/en/function.substr.php It is what I used to do the same thing.  I am new to php but from what I gather, you tell it where to start and how much to keep.  If the length changes often then you could just use the size of the variable to tell it to keep all but the last letter.  I have no clue as to how efficient it is though.

 

I should have read it first.

 

$rest = substr("abcdef", 0, -1);  // returns "abcde"

<?php
$string = "Shows";
$string = rtrim($string, 's');
echo $string;
?>

 

If you want a more comprehensive solution that will strip ANY word of a trailing 's':

<?php
$string = "Here, we have some words with letters on the end.  Enjoy the shows!";
$string = preg_replace('/([a-z]+?)s(?=\s|(\b.)?$)/i', '$1', $string);
echo $string;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.