duanji2002
2017-06-07 22:12
浏览 28
已采纳

如何使用有条件允许的字符串验证输入字符串?

I want to block some words from being used in a username unless they are a substring of a permitted string.

The words love, hug, and kiss are not permitted, unless they are used in mylovely,givehug,kissing.

I started with this pattern, but it doesn't work:

preg_match('[love|hug|kiss]', $data)

These are some inputs with the desired outputs:

  • boolove (invalid - "love" found, but not in "mylovely")
  • mylovely (valid - "love" contained in "mylovely")
  • givehug (valid - "hug" contained in "givehug")
  • igivehug (valid - "hug" contained in "givehug")
  • kissing (valid - "kiss" contained in "kissing")
  • youhug (invalid - "hug" found, but not in "givehug")
  • ikiss (invalid - "kiss" found, but not in "kissing")
  • Basit (valid - doesn't contain love, hug, or kiss)

How can I achieve this validation to return a true|false response?

图片转代码服务由CSDN问答提供 功能建议

我想阻止某些单词在用户名中使用,除非它们是允许字符串的子字符串。

不允许使用 love hug kiss ,除非在中使用它们 mylovely givehug kissing

我开始使用这种模式,但它不起作用:

  preg_match('[love | hug | kiss]',$ data)
   
 
 

这些是一些输入 期望的输出:

  • boolove(无效 - 发现“爱情”,但不是“mylovely”)
  • mylovely(有效 - “爱情” “包含在”mylovely“)
  • givehug(有效 - ”抱抱“包含在”givehug“中)
  • igivehug(有效 - ”抱抱“包含在”givehug“中 )
  • 接吻(有效 - “吻”包含在“接吻”中)
  • youhug(无效 - “拥抱”发现,但不是“赠送”)
  • ikiss(无效 - 发现“吻”,但不接受“亲吻”)
  • Basit(有效 - 不包含 <爱,拥抱或亲吻)

    如何实现此验证以返回真实的错误回复?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudi1978 2017-06-07 22:56
    已采纳

    My non-regex method has a potential flaw exposed when two $flag values exist, but one $forgive value exists, so I wish to provide a regex method that uses a pattern with lookbehinds and lookaheads to identify invalid strings:

    Code (PHP Demo , Pattern Demo):

    $inputs=['love','mickmackusa','kisses','mylovely','give','hug','givehug','kiss','kissing'];
    foreach($inputs as $input){
        echo "
    $input ";
        var_export(preg_match('/kiss(?!ing)|(?<!give)hug|(?<!my)love(?!ly)/i',$input)==1?false:true);
    }
    

    Output:

    love false
    mickmackusa true
    kisses false
    mylovely true
    give true
    hug false
    givehug true
    kiss false
    kissing true
    

    Old non-regex method...

    Input:

    $input='code'; // mylovely(true) , give(false) , code(true)
    $checks=['love'=>'mylovely','give'=>'givehug','kiss'=>'kissing'];
    

    Method (no regex):

    $result=true;
    foreach($checks as $flag=>$forgive){
        if(stripos($input,$flag)!==false && stripos($input,$forgive)===false){
            $result=false;  // unforgivable word included
            break;
        }
    }
    var_export($result);
    

    Output:

    true
    

    Explanation:

    • The $checks array has keys that represent words that may be invalid, and values that are permitted if the key is matched in the $input value.
    • The 3 $check elements are iterated and stripos() is used to case-insensitively check for input validity.
    • For efficiency, as soon as $input is deemed invalid, the loop is broken.
    点赞 打赏 评论

相关推荐 更多相似问题