Danila Ravinsky
Danila Ravinsky

Reputation: 5

delete 's' from all values except that have a digits before 's' in the end of each word

Girl*s* Boy*s* Dog*s* s* 1234567890*s*

my code is look like this

<?php
$tags = "john123s ewggw1s friend's piter or girls jumps john's september";
$wordlist = array("or", "and", "where", "a", "the", "for", "is", "out", "!", "?" ,"," ,"." , "' '");

foreach ($wordlist as &$word) {
    $word = '/\b' . preg_quote($word, '/') . '\b/';



}
$tags = preg_replace($wordlist, '', $tags);
$words = $tags;
$output = str_replace("'", "", $words);

$output = preg_replace("/s\b/", "", $output);


echo $output;
?>

it not ignores john123s and ewggw1s, i tried to write if, but nothing is working...

Upvotes: 0

Views: 44

Answers (2)

Salketer
Salketer

Reputation: 15711

$output = preg_replace("/([^\d])s\b/", "$1", $output);

Using the ^ inside brackets means pick anything but NOT this kind of character. Since that means it will also select that letter so we need to grab it in a var using the ( ) and replace that (2 letter) string by the captured letter, so "$1" instead of ""

I suggest you play with it using the functions-online website... I always do that, easy and fast to test code. http://www.functions-online.com/preg_replace.html

This code would remove the s that are preceded by a ' so Henry's cookies would become Henry' cookie so I suggest you add the ' character in the negation pattern, making it like this:

$output = preg_replace("/([^\d'])s\b/", "$1", $output);

And you would then remove the line so you keep the ' characters...:

$output = str_replace("'", "", $words);

Unless you are really expecting something special out of it, there is no reason to butcher english like so... Do not forget that Dismiss, and a lot of other words finish in s not just 3rd person verbs and plurial nouns...

Upvotes: 0

omma2289
omma2289

Reputation: 54639

You are looking for the negative lookbehind /(?<!x)y/ this means find all "y" not preceded by "x"

$output = preg_replace("/(?<![0-9])s\b/",'',$output);

Upvotes: 1

Related Questions