Current Location: Home> Latest Articles> Clean up user-submitted form data

Clean up user-submitted form data

M66 2025-05-14

In web development, cleaning and verification of form data is an important step to ensure data security and accuracy. PHP's array_filter function provides us with a simple and efficient way to filter elements in an array. By combining array_filter and custom callback functions, we can clean up the form data submitted by the user and verify it before processing the data.

1. Introduction to array_filter function

The array_filter function is used to filter elements in an array and return a new array containing elements tested by the callback function. The basic syntax is as follows:

 array_filter(array $array, ?callable $callback = null, int $mode = 0): array
  • $array : The input array to filter.

  • $callback : A callback function used to filter array elements. If the callback function is not provided, elements with the value FALSE (such as 0 , NULL , FALSE , empty string "" , etc.) will be deleted by default.

  • $mode : Specify the filtering method. Usually there is no need to set it, just use the default value 0 .

2. Use array_filter to clean form data

Suppose we have a form where the user has submitted information such as username, email, and mobile phone number. We want to clean up this data and remove unnecessary spaces or illegal characters.

Sample code:

 <?php
// User-submitted data
$formData = [
    'username' => ' john_doe ',
    'email' => ' john.doe@m66.net ',
    'phone' => ' 123-456-7890 ',
    'address' => '  '
];

// Clean up data functions:Remove spaces and illegal characters
$cleanData = array_map('trim', $formData);

// Output the cleaned data
print_r($cleanData);
?>

In the above code, array_map('trim', $formData) removes the spaces before and after each form field value through the trim function. If you want to further clean and verify the data, you can use array_filter to remove fields that do not meet the requirements.

3. Use array_filter for data verification

Verifying form data is a critical step to ensure it meets the requirements. For example, we may want to make sure that the username has no spaces, the email format is correct, or the phone number is valid.

Sample code:

 <?php
// User-submitted data
$formData = [
    'username' => ' john_doe ',
    'email' => ' john.doe@m66.net ',
    'phone' => ' 123-456-7890 ',
    'address' => '  '
];

// Clean up data:Remove excess spaces
$cleanData = array_map('trim', $formData);

// Verify data:usearray_filterFilter illegal data
$validData = array_filter($cleanData, function($value, $key) {
    switch ($key) {
        case 'username':
            return preg_match('/^\S+$/', $value);  // Make sure there are no spaces in the username
        case 'email':
            return filter_var($value, FILTER_VALIDATE_EMAIL);  // Verify the mailbox format
        case 'phone':
            return preg_match('/^\d{3}-\d{3}-\d{4}$/', $value);  // Verify mobile phone number format
        default:
            return !empty($value);  // Other fields cannot be empty
    }
}, ARRAY_FILTER_USE_BOTH);

// Output valid data
print_r($validData);
?>

In this example, we use array_filter in conjunction with callback function to verify each form field. The callback function checks whether the content of each field complies with a specific rule:

  • The username field ensures that there are no spaces.

  • The email field ensures that the format meets the standards of email.

  • The phone field ensures that the phone number meets a specific format (for example, 123-456-7890 ).

If the field does not meet the requirements, array_filter will filter out the illegal data and return a new array with only legal fields.

4. Process invalid data

If you want to give a prompt when the user submits invalid data, you can do it by checking whether $validData is empty.

Sample code:

 <?php
// Assumptions$validDataIt has been generated through the above code
if (empty($validData)) {
    echo "All submitted data are invalid,Please check your input。";
} else {
    echo "Data verification passed!";
    // Further process valid data
}
?>

If the array $validData is empty, it means that all data submitted by the user does not comply with the verification rules. You can feedback to the user based on this result.