您可以用忽略具有前导和/或尾随字母的单词的 a 替换 your str_replace(),preg_replace因此只有当它单独存在时才会替换发誓词:
$post = "some Scunthorpe text";
$newpost = $post;
$swearWords = file("blacklist.txt");
foreach ($swearWords as $naughty)
{
$naughty = preg_quote($naughty, '/');
$newpost = preg_replace("/([^a-z]+{$naughty}[^a-z]*|[^a-z]+{$naughty}[^a-z]+)/i", "<b><i>(oops)</i></b>", $newpost);
}
if ($newpost) $post = $newpost;
else echo "an error occured during regex replacement";
请注意,它仍然允许使用诸如“aCUNT”、“soFUCKINGstupid”之类的脏话......我不知道你怎么能处理它。