tags:

views:

127

answers:

5

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?

A: 

You are looking for something like:

foreach ($array as $value) {
   //do checking here
}
Kitson
Kitson, I can loop through the values just fine but I need help with the logic.
... and the best practice on what I'm doing. Thanks
+3  A: 

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.

Chacha102
Hey Chacka, thanks. You actually helped me the other day; I remember you. Thanks, you nailed it.
Updated for the Numeric Values too. No problem. I keep most of my answers in a personal wiki anyways, so this will help me in future programs.
Chacha102
Chacha, just curious, what is the strpos function for? Thanks again.
it returns the position of a substring within a string, or false it it doesn't exist. Here it checks whether there is a space in $id or not. see http://php.net/strpos
Tom Haigh
A: 

array_filter could be an option.

whichdan
Hey whichdan, thanks for the link, I never used that function before but it certainly can be useful. Thanks for sharing. :)
A: 

The user is inputing several email addresses, separated with semicolons. The first thing you do is to filter that string and strip out all white spaces. Then you explode it and loop over the array and do the regular valid email checks, dns lookups, etc. for each entry. No special treatment before the explode needed except the white space trim.

tharkun
Thanks tharkun. Sometimes I get lost in all the logic going on and need someone to put me back on track. :)
A: 

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
}
?>
phidah
Hi phidah, I already implemented the changes with the exception of the strpos function. What is that for? BTW: Thanks for the array_filter code.