bklups
bklups

Reputation: 117

Delete last letter of each word if it's letter "s"

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

Answers (2)

feeela
feeela

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 );

http://php.net/rtrim

Upvotes: 1

Itchy
Itchy

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

Related Questions