Reputation: 117
As indicated in the title, I have a sentence with some words and I want to delete last letter if it is letter s
for each word of the sentence.
I try this
preg_replace("%s(?!.*s.*)%", "", $mystring);
But it delete last word only
Upvotes: 1
Views: 1515
Reputation: 29932
I think I wouldn't even use regexp to do so.
$output = array();
foreach( explode( ' ', $myString ) as $word )
{
$output[] = rtrim( $word, 's' );
}
$myString = implode( ' ', $output );
Upvotes: 1
Reputation: 2424
Try "s\\b"
as regular expression
Example:
preg_replace("/s\b/", "", $mystring);
s
is your letter and \b
means a word-boundary.
Upvotes: 16