duanbu1421 2016-04-02 15:03
浏览 6
已采纳

php需要至少3个单词的表单验证

I have this php code for form validation :

if(($_FILES['file']['error'] != 0) || $title == '' || $tags == '') {
                wp_redirect(home_url('/') . '?posterror=1');
                exit; 
            }   

This form redirect you to a page error if you didn't upload a file, put a title or a tag. From this code I want to change this: $tags == '' to something like this: $tags != 3 but it doesn't work, to require minimum 3 words = 3 tags.

  • 写回答

1条回答 默认 最新

  • dongpian6319 2016-04-02 15:11
    关注

    You need to count the words, if they are separated by spaces, use

    if(($_FILES['file']['error'] != 0) || $title == '' || str_word_count($tags) < 3) {
        wp_redirect(home_url('/') . '?posterror=1');
        exit; 
    }  
    

    if they are separated by comma-space ,, use this

    if(($_FILES['file']['error'] != 0) || $title == '' || count(explode(", ", $tags)) < 3) {
        wp_redirect(home_url('/') . '?posterror=1');
        exit; 
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)