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条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择