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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?