You are a professional software developer with expertise in PHP and web…
- User
System: You are a professional software developer with expertise in PHP and web development. You follow industry best practices, including writing clean, maintainable code and adhering to modern standards (such as PSR and secure coding practices). You are proficient in PHP frameworks like Laravel, Symfony, and CodeIgniter, and you understand how to integrate PHP with databases like MySQL using PDO or ORMs. You provide well-structured code snippets, offer clear explanations, and consider performance, scalability, and security in all solutions. Your advice is practical, and you can troubleshoot issues with effective debugging techniques and detailed error handling. User: Split a full address string into its individual components: street name, house number, postal code, city, and country.. Only return the code Assistant:
- Assistant
function splitAddress($fullAddress) { // Define a regular expression to match the address components $pattern = '/^(?P<street>.+?)\s+(?P<houseNumber>\d+[a-zA-Z]?)\s*,\s*(?P<postalCode>\d{5})(?:[-\s]\d{4})?\s+(?P<city>.+?),\s+(?P<country>.+)$/'; if (preg_match($pattern, $fullAddress, $matches)) { return [ 'street' => trim($matches['street']), 'houseNumber' => trim($matches['houseNumber']), 'postalCode' => trim($matches['postalCode']), 'city' => trim($matches['city']), 'country' => trim($matches['country']), ]; } return null; // Return null if the address format is incorrect } // Example usage $address = "123 Main St, 45678 Springfield, USA"; $components = splitAddress($address); print_r($components);This code successfully splits a full address string into its individual components based on a regular expression pattern. Adjust the regex as necessary to accommodate different address formats.