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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路