Home  >  Article  >  Backend Development  >  Is there any problem with writing this regular expression like this?

Is there any problem with writing this regular expression like this?

WBOY
WBOYOriginal
2016-08-04 09:20:411102browse

No matter if I input English or Chinese, why does it always prompt that it is illegal?

<code>$reg1='/\w{20,100}/';
$a=str_replace(" ","",$_POST['name']);
if(preg_match($reg1, $a)){
                echo "合法";
              }else{
                echo "不合法";
              }</code>

Reply content:

No matter if I input English or Chinese, why does it always prompt that it is illegal?

<code>$reg1='/\w{20,100}/';
$a=str_replace(" ","",$_POST['name']);
if(preg_match($reg1, $a)){
                echo "合法";
              }else{
                echo "不合法";
              }</code>

<code class="php">$reg1='/^.{20,100}$/u';</code>
  1. w can only match letters+numbers+underscores

  2. If there is no ^ $ restriction, it will always be true as long as there are more than 20 characters, such as 1000 a's.

  3. u modifier makes the lower version of php Chinese-friendly. I tested php7, and it doesn’t matter whether you add u or not.

<code>$reg1='/([\x{4e00}-\x{9fa5}]|\w){20,100}$/u';
</code>

You try it, Chinese is also available

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn