dtrvzd1171 2011-07-26 01:51
浏览 45
已采纳

尝试设置无效字符数组时出现语法错误

I'm fairly new to PHP, and I'm setting up a "members area" for my site following this tutorial. There seems to be a problem with the code near the bottom of the page.

When trying to pick out the invalid characters from a username field, the array $junk is set up.

$junk = array('.' , ',' , '/' , '\' , '`' , ';' , '[' ,  ']' , '-', '_', '*', '&', '^',
'%', '$', '#', '@', '!', '~', '+', '(', ')', '|', '{', '}', '<', '>', '?', ':', '"', '='); 

This block of code returns a syntax error for me.

Now, my first thought was that either the '#' or the '(' and ')' was causing the problem, since they are commonly used in html and php. The error was still returned with them removed.

I am new to php, so it very well could be just some small syntax error I am missing. Any input would be great. Thanks!

EDIT: Also, if there is an easier way of doing this, please let me know!

  • 写回答

4条回答 默认 最新

  • duanou9758 2011-07-26 02:01
    关注

    There is a much easier way to do this, regular expressions. If you want to set up the username to only accept alpha-numeric characters it's,

    $user = preg_replace("/[^0-9a-zA-Z]+/", "", $user);

    The expression essentially says, replace anything that is not 0-9, a-z, A-Z with "". If you want to allow the "@", "." (period) and "-" and "_" symbols:

    $user = preg_replace("/[^0-9a-zA-Z\@\.\-\_]+/", "", $user);
    

    Regular expressions are a very powerful tool. They can be used in most languages, including javascript and PHP. So it is worth taking the time to learn them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?