【发布时间】:2015-03-06 17:36:57
【问题描述】:
我有以下网址,其中 var_a 应该始终是一个数字。我也不知道 var_a 会有多少:
http://localhost/url.php?var_a[]=298&var_a[]=299
如果用户在 url 中写入特殊字符,如何防止出错?
我已经这样做了:
$data = preg_replace('/[^0-9\']/', '',$_GET['var_a']);
$data = str_replace("'", '', $data);
print_r ($data);// Array ( [0] => 298 [1] => 299 )
//This is ok, all number were printed
但我仍然收到# % & 等特殊字符的错误
例如
http://localhost/url.php?var_a[]=298&var_a[]=#299
print_r ($data);// Array ( [0] => 298 [1] => )
//299 was not printed!
我该如何解决这个问题?
【问题讨论】:
-
使用urlencode?
标签: php preg-replace special-characters