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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-04 09:20:41
Original
1155 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

<code class="php">$reg1='/^.{20,100}$/u';</code>
Copy after login
  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>
Copy after login

You try it, Chinese is also available

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template