dt56449492 2010-07-07 06:15
浏览 86
已采纳

从ereg更改为preg_match时,PHP表达式失败

I have a class that uses PHP's ereg() which is deprecated. Looking on PHP.net I thought I could just get away and change to preg_match()

But I get errors with the regular expressions or they fail!!

Here are two examples:

function input_login() {
    if (ereg("[^-_@\.A-Za-z0-9]", $input_value)) { // WORKS
    // if (preg_match("[^-_@\.A-Za-z0-9]", $input_value)) { // FAILS
        echo "is NOT alphanumeric with characters - _ @ . allowed";
    }
}

// validate email
function validate_email($email) {
    // return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email); // FAILS
}
  • 写回答

3条回答 默认 最新

  • duancheng1955 2010-07-07 06:17
    关注

    You forgot the delimiters:

    if (preg_match("/[^-_@.A-Za-z0-9]/", $input_value))
    

    Also, the dot doesn't need to be escaped inside a character class.

    For your validation function, you need to make the regex case-insensitive by using the i modifier:

    return preg_match('/^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i', $email)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改