Home  >  Q&A  >  body text

Prevent consecutive meaningless characters from occurring in JS or PHP

Users often send many meaningless characters such as "Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"

Is there any way to prohibit it? Either js or php is fine.

Thanks

習慣沉默習慣沉默2645 days ago703

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-29 10:10:06

    Regular judgment

    var re = /(.){3,}/u;  // 匹配同一个字符4次以上
    re.test(str); // true = 同一字符4次或以上
    preg_match('/(.){3,}/u', $str); // 1 = 同一个字4次或以上

    Send a JS test picture:

    Note:

      The
    1. u

      modifier after the regular is used to support Unicode wide characters

    2. {3,} means matching 3 times or more. Since the previous (.) has already been matched once, the entire limited number is 1 greater than this number. In addition, this number can be changed according to your own needs.

    reply
    0
  • Cancelreply