PHP用短语替换脏话 [英] PHP Replacing swear words with phrases

查看:189
本文介绍了PHP用短语替换脏话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我了解了如何用其他单词替换某些单词.我想弄清楚的是如何将一个单词替换为一个短语并消除所有其他输入.

So I get how to replace certain words with other ones. What I'm trying to figure out is how to take a word and replace it with a phrase and eliminate all other input.

例如:

坏词是狗"

用户输入->你闻起来像狗."

user inputs -> 'You smell like a dog.'

而不是用彩虹"代替狗",我希望它回显诸如你是个便盆"之类的东西.

instead of it replacing 'dog' with 'rainbow' or something, I want it to echo something like: 'You are a potty mouth'.

这就是我要编写的代码:

Here's what I have for code:

<?php

$find = array('dog', 'cat', 'bird');
$replace = 'You are a potty mouth.';

if (isset ($_POST['user_input'])&&!empty($_POST['user_input'])) {
    $user_input = $_POST['user_input']; 
    $user_input_new = str_ireplace($find, $replace, $user_input);

        echo $user_input_new;
}
?>

使用此代码,它回显:你闻起来像是个傻瓜."

With this code it echos: 'You smell like a You are a pottymouth.'

我确定这是转贴,我深表歉意.我所能找到的只是关于如何仅替换部分字符串而不是全部字符串的文档.

I'm sure this is a repost and I apologize. Everything I've been able to find is documentation on how to replace only parts of strings, not entire ones.

推荐答案

在这种情况下,您可以检查用户输入字符串中是否有坏词",如果返回"true",则回显"You are便盆."

Well, in this case you can just check whether there is a "bad word" in the user input string, and if it returns true, echo "You are a potty mouth."

您将要使用strpos()

You would want to use strpos()

例如

if( strpos($_POST['user_input'],'dog')!==FALSE ) {
    echo('You are a potty mouth');
}

如果您有一系列坏词",则需要遍历它们以检查用户输入中是否有任何不正确的词.

If you have an array of "bad words" you'll want to loop through them to check any occur within user input.

这篇关于PHP用短语替换脏话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆