Php 如何拒绝用户输入的某些关键字

Php 如何拒绝用户输入的某些关键字,php,arrays,validation,Php,Arrays,Validation,应拒绝的用户输入示例: 出售房屋 租车 WTB价格便宜的iphone 如何使我的代码拒绝上述输入 $title = array('rent','buy','sale','sell','wanted','wtb','wts'); $user_title = stripslashes($_POST['title']); if (in_array($user_title, $title)) { $error = '<p class="error">Do not include

应拒绝的用户输入示例:

  • 出售房屋
  • 租车
  • WTB
    价格便宜的iphone
如何使我的代码拒绝上述输入

$title = array('rent','buy','sale','sell','wanted','wtb','wts');
$user_title = stripslashes($_POST['title']);
if (in_array($user_title, $title)) {
    $error = '<p class="error">Do not include ' . $user_title . ' on your title</p>';
}
$title=array('rent','buy','sale','sell','wanted','wtb','wts');
$user_title=stripslashes($_POST['title']);
if(在数组中($user\u title,$title)){
$error='

不要在标题上包含“$user\u title.”; }

您可以使用:

$title=array('rent','buy','sale','sell','wanted','wtb','wts');
$user_title=stripslashes($_POST['title']);
foreach($word形式的标题)
{
if(stripos($user\u title,$word)!==false)
{
$error='

不要在标题上包含“$word.”; 打破 } }

您可以使用:

$title=array('rent','buy','sale','sell','wanted','wtb','wts');
$user_title=stripslashes($_POST['title']);
foreach($word形式的标题)
{
if(stripos($user\u title,$word)!==false)
{
$error='

不要在标题上包含“$word.”; 打破 } }


您也可以使用正则表达式:

if (preg_match("/(rent|buy|sale|sell|wanted|wtb|wts)/is", $user_title)) {
    $error = '<p class="error">Do not include ' . $user_title . ' on your title</p>';
}
if(preg|u match(/(租金|买|卖|卖|通缉| wtb | wts)/is“,$user|title)){
$error='

不要在标题上包含“$user\u title.”; }


您也可以使用正则表达式:

if (preg_match("/(rent|buy|sale|sell|wanted|wtb|wts)/is", $user_title)) {
    $error = '<p class="error">Do not include ' . $user_title . ' on your title</p>';
}
if(preg|u match(/(租金|买|卖|卖|通缉| wtb | wts)/is“,$user|title)){
$error='

不要在标题上包含“$user\u title.”; }

您可以利用explode()来分离$user\u title中的单词,并检查每个单词以确保$title中不存在该单词

$invalidWords = '';

$words = explode(' ', stripslashes($_POST['title']));
foreach($words as $word) {
    if (in_array($word, $title)) {
        $invalidWords .= ' ' . $word;
    }
}

if (!empty($invalidWords)) {
    echo '<p class="error">Do not include the following words in your title: ' . $invalidWords . '</p>';
}
$invalidWords='';
$words=explode(“”,带斜杠($_POST['title']);
foreach($words作为$word){
if(在数组中($word,$title)){
$invalidWords.=''.$word;
}
}
如果(!空($invalidWords)){
echo“

不要在标题中包含以下单词:”.$invalidWords.“

”; }
RegEx可能是最好的,但我无法立即找出所需的表达式,以便您能够将列表中的所有无效单词输出给用户。

您可以使用explode()来分离$user\u title中的单词,并检查每个单词以确保它不存在于$title中

$invalidWords = '';

$words = explode(' ', stripslashes($_POST['title']));
foreach($words as $word) {
    if (in_array($word, $title)) {
        $invalidWords .= ' ' . $word;
    }
}

if (!empty($invalidWords)) {
    echo '<p class="error">Do not include the following words in your title: ' . $invalidWords . '</p>';
}
$invalidWords='';
$words=explode(“”,带斜杠($_POST['title']);
foreach($words作为$word){
if(在数组中($word,$title)){
$invalidWords.=''.$word;
}
}
如果(!空($invalidWords)){
echo“

不要在标题中包含以下单词:”.$invalidWords.“

”; }

RegEx可能是最好的,但我无法立即找出所需的表达式,以便您能够将列表中的所有无效单词输出给用户。

如果您希望被拒绝的单词是完整的单词,而不是另一个单词的一部分,则被视为被拒绝,您可以使用基于正则表达式的解决方案来设置单词边界:

// array of denied words.
$deniedWords = array('rent','buy','sale','sell','wanted','wtb','wts');

// run preg_quote on each array element..as it may have a regex meta-char in it.
$deniedWords = array_map('preg_quote',$deniedWords);

// construct the pattern as /(\bbuy\b|\bsell\b...)/i
$pat = '/(\b'.implode('\b|\b',$deniedWords).'\b)/i';

// use preg-match_all to find all matches
if(preg_match_all($pat,$user_title,$matches)) {

    // $matches[1] has all the found word(s), join them with comma and print.
    $error = 'Do not include ' . implode(',',$matches[1]);    
}

如果您希望被拒绝的单词是完整的单词,而不仅仅是另一个单词的一部分,那么您可以使用基于正则表达式的解决方案来定义单词边界:

// array of denied words.
$deniedWords = array('rent','buy','sale','sell','wanted','wtb','wts');

// run preg_quote on each array element..as it may have a regex meta-char in it.
$deniedWords = array_map('preg_quote',$deniedWords);

// construct the pattern as /(\bbuy\b|\bsell\b...)/i
$pat = '/(\b'.implode('\b|\b',$deniedWords).'\b)/i';

// use preg-match_all to find all matches
if(preg_match_all($pat,$user_title,$matches)) {

    // $matches[1] has all the found word(s), join them with comma and print.
    $error = 'Do not include ' . implode(',',$matches[1]);    
}

最好使用
stripos()
但是,否则它会因为不使用regex而错过
RENT
+1,这不是一件坏事,只是有点慢:-)regexp实际上会随着关键字数量的增加而快得多。不幸的是,使用它也会阻止您使用包含任何这些单词的单词作为子字符串,例如“布伦特伍德”或“特伦顿”。即使如此,这也在假装子字符串黑名单的整个想法实际上是一种防止用户在你的网站上销售的更为可行的方式…@Josh@Lotus:我明白了,codeaddict的解决方案不仅效果更好,而且速度更快(我已经分析过了,我的(修改为收集所有单词)是~400µs,codeaddict是~280µs)。不过最好使用
stripos()
,否则它会因为不使用正则表达式而错过
RENT
+1,这并不是一件坏事,它只是有点慢:-)随着关键字数量的增加,regexp实际上会快得多。不幸的是,使用它也会阻止您使用包含任何这些单词的单词作为子字符串,例如“Brentwood”或“Trenton”。即使如此,这也在假装子字符串黑名单的整个想法实际上是一种防止用户在你的网站上销售的更为可行的方式…@Josh@Lotus:我明白了,codeaddict的解决方案不仅效果更好,而且速度更快(我已经分析过了,我的(修改为收集所有单词)是约400µs,codeaddict是约280µs)。我希望这只是一个练习,因为防止垃圾邮件肯定比搜索子字符串更复杂。您可能需要使用Akismet之类的插件。我希望这只是一个练习,因为防止垃圾邮件肯定比搜索子字符串更复杂。您可能想使用Akismet之类的插件。+1,这是我想做的解决方案,但没有时间。非常优雅,伸缩性好。+1,这是我想做的解决方案,但没有时间。非常优雅和规模良好。