What are the best practices for implementing customized address lines in PHP forums or websites?

When implementing customized address lines in PHP forums or websites, it is important to sanitize user input to prevent SQL injection attacks and validate the data to ensure it meets the required format. One way to achieve this is by using PHP functions like filter_var() to sanitize and validate the input before storing or displaying it on the website.

// Sanitize and validate the custom address line input
$customAddress = filter_var($_POST['custom_address'], FILTER_SANITIZE_STRING);

// Check if the input meets the required format
if(preg_match('/^[a-zA-Z0-9\s#.,-]*$/', $customAddress)) {
    // Store the sanitized input in the database or use it in the website
} else {
    // Display an error message to the user
    echo "Invalid address format. Please enter a valid address.";
}