女孩*s* 男孩*s* 狗*s
* s* 1234567890*s*
我的代码看起来像这样
<?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;
?>
它不会忽略 John123s 和 ewggw1s,我试图写 if,但没有任何效果......
您正在寻找负面的后视/(?<!x)y/
这意味着找到所有"y"前面没有"x"
$output = preg_replace("/(?<![0-9])s'b/",'',$output);
$output = preg_replace("/([^'d])s'b/", "$1", $output);
在括号内使用 ^ 意味着选择任何东西,但不是这种字符。由于这意味着它还将选择该字母,因此我们需要使用 ( )
在 var 中抓取它,并将该(2 个字母)字符串替换为捕获的字母,因此"$1"
而不是""
我建议你使用功能在线网站玩它......我总是这样做,简单快速地测试代码。http://www.functions-online.com/preg_replace.html
这段代码将删除前面有'
的s
,因此Henry's cookies
会变得Henry' cookie
所以我建议你在否定模式中添加 ' 字符,使其如下所示:
$output = preg_replace("/([^'d'])s'b/", "$1", $output);
然后,您将删除该行,以便保留" 字符...:
$output = str_replace("'", "", $words);
除非你真的期待它有什么特别的东西,否则没有理由像这样屠杀英语......不要忘记Dismiss
,以及许多其他单词以s
结尾,而不仅仅是第三人称动词和多语名词......