douzen3516 2012-03-11 07:12
浏览 26
已采纳

无法弄清楚如何使substr_compare正常工作(PHP)

So I have this basic script:

/*check if email is valid*/
if (substr_compare ( $email , '@' , 0 )>0)
{   
/*put user to temporary database*/      
echo 'registration script';
}else{echo 'wrong mail';};

What I want to do is I want to check if string contains symbol '@' (if mail is valid). But it does not work: when I set up variable $email to something like 'name' = the function returns '1', though I assume it should return 0 or -1, as the letter is not there. What am I missing?

  • 写回答

3条回答 默认 最新

  • donglu5000 2012-03-11 07:23
    关注

    To check if the sctring contains another substring you need to use strpos (not compare function such as substr_compare):

    if (strpos($email, '@') !== null) { ... }
    

    And yes, the way you check for email address is pretty weak. Use regular expressions or built in PHP filters like

    if (filter_var($email, FILTER_VALIDATE_EMAIL)) { ... }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数