0

I must be missing something... This is suppose to do supplementary validation of optional profile fields. This expects common stuff like trim() and strip_tags() to have already been run. Params are passed by-ref, so I can set them to an empty string if any fail.

I thought this was correct, maybe there is an easier way to achieve this, If anyone can explain to me where I lost my way.

function validate_optional_fields(&$website, &$location, &$occupation, &$interests, &$sig, &$facebook)
{
        $check_var_length = ['location', 'occupation', 'interests', 'sig', 'facebook'];
        
        for($i = 0; $i < count($check_var_length); $i++)
        {
                if (strlen((string) ${$check_var_length}[$i]) < 2)
                {
                        ${$check_var_length}[$i] = '';
                }
        }
        // website has to start with http://, followed by something with length at least 3 that
        // contains at least one dot.
        if ($website != "")
        {
                if (!preg_match('#^http[s]?:\/\/#i', (string) $website))
                {
                        $website = 'http://' . $website;
                }

                if (!preg_match('#^http[s]?\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', (string) $website))
                {
                        $website = '';
                }
        }

        return;
}

1 Answers1

1

The error is to use ${$check_var_length}[$i] instead of ${$check_var_length[$i]}.

But, you could make the test easier by using foreach().

$check_var_length = ['location', 'occupation', 'interests', 'sig', 'facebook'];    
foreach ($check_var_length as $var) {
    if (strlen($$var) < 2) {
        $$var = '';
    }
}
    
Syscall
  • 19,327
  • 10
  • 37
  • 52