开发者

preg_match question

开发者 https://www.devze.com 2023-01-18 10:10 出处:网络
$page = $_GET[\'page\']; if(isset($page)) if(!preg_mat开发者_运维知识库ch(\'/[\\w\\d_]+/i\', $page)) die(\"Error\");
$page = $_GET['page'];
if(isset($page))
if(!preg_mat开发者_运维知识库ch('/[\w\d_]+/i', $page)) die("Error");

I want to allow alphanum and underscore,

above code works but let say i set 123..., this works too. Is preg_match will not validate behind entry?


The regex will match as long as an alphanumeric appears as a substring of $page. Since 123... contains the substring 123 it will pass your regex.

Use

/^\w+$/

to match the whole string. (\w already means [a-zA-Z0-9_] so your \d, _ and the i modifier are redundant.)


You need to use anchors as:

/^\w+$/

\w already has \d and _

0

精彩评论

暂无评论...
验证码 换一张
取 消