duankan6894
2009-07-27 05:57 阅读 51
已采纳

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 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.

    点赞 评论 复制链接分享
  • dspows0637 dspows0637 2009-07-27 06:00

    You are looking for something like:

    foreach ($array as $value) {
       //do checking here
    }
    
    点赞 评论 复制链接分享
  • doupa1883 doupa1883 2009-07-27 06:07

    array_filter could be an option.

    点赞 评论 复制链接分享
  • duangao8359 duangao8359 2009-07-27 08:19

    As whichdan suggested, here is an implementation that relies on array_filter():

    <?php
    function checkValue($value)
    {
        $id = trim(id);
        if(!is_numeric($id))
        {
            return false;
        }
        if(strpos(" ", $id) !== false)
        {
            return false;
        } 
        if(!in_array($id, $database_ids))
        {
            return false;
        }
        return true;
    }
    
    $values = '1;2;3;4';
    $values_extracted = explode(';', $values);
    
    if(count($values) == count(array_filter($values_extracted), 'checkValue'))
    {
       // Input was successfully validated
    }
    ?>
    
    点赞 评论 复制链接分享

相关推荐