duankan6894 2009-07-27 05:57
浏览 81
已采纳

PHP - 有没有办法验证数组中的所有值

Using PHP..

Here is what I have.. I'm gonna explain the whole thing and maybe someone can help me with the logic and maybe point me in the right direction.

I have a mail system I am working on. In the cc part, I am allowing the user to seperate the values by a semicolon, like so: 1;2;3;4...

When these values are passed to my function, I am using explode to get them into an array. What I want to do is some checking first. I want to firstly make certain that the format is correct and that every value is correctly seperated. If not, I should show an error. Once this is done, I want to make certain that every number is actually valid. I can query the database, put the reslts into an array and was thinking to use the in_array() function to verify this but I'm not certain that it will work. Can someone please help me out with the best way to handle this?

Thanks.

EDIT:

What is the best way to detect a bogus value in the CSV list of values?

  • 写回答

4条回答 默认 最新

  • douyu1990 2009-07-27 06:03
    关注

    In order to verify that each number was correct seperated, you want to check that there is no whitespace in the answer. So something like this should work:

    $text = trim($id);
    if(strpos(" ", $id) !== false)
    {
        //error
    }
    

    Next, to check for the values, it is very simple

    if(!in_array($id, $database_ids))
    {
        // error
    }
    

    Finally, if you are only using numeric values, check that the id is numeric

    if(!is_numeric($id))
    {
        //error
    }
    
    

    To combine, wrap it into an array

    foreach($exploded_array as $key => $id)
    {
        $id = trim(id);
        if(!is_numeric($id))
        {
            //error
        }
        if(strpos(" ", $id) !== false)
        {
            //error
        } 
        if(!in_array($id, $database_ids))
        {
            // error
        }
    }
    

    I hope the code was pretty self explanatory where it got the variables, but if you need me to explain more, feel free to ask.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?