/** * Clean and normalize phone number - removes +1 prefix and formats to 10 digits * Handles: +17048043320, 17048043320, (704) 804-3320, 704-804-3320, etc. * * @param string $phone Raw phone number input * @return string Clean 10-digit phone number (e.g., "7048043320") */ function cleanPhoneNumber($phone) { // Remove all non-numeric characters except + $cleaned = preg_replace('/[^\d+]/', '', $phone); // Remove leading + if present $cleaned = ltrim($cleaned, '+'); // If 11 digits starting with 1, remove the leading 1 (US country code) if (strlen($cleaned) === 11 && $cleaned[0] === '1') { $cleaned = substr($cleaned, 1); } // Keep only last 10 digits if more than 10 if (strlen($cleaned) > 10) { $cleaned = substr($cleaned, -10); } return $cleaned; } /** * Format phone number as (XXX) XXX-XXXX for display * * @param string $phone Phone number (raw or cleaned) * @return string Formatted phone number */ function formatPhoneNumber($phone) { $cleaned = cleanPhoneNumber($phone); if (strlen($cleaned) === 10) { return '(' . substr($cleaned, 0, 3) . ') ' . substr($cleaned, 3, 3) . '-' . substr($cleaned, 6, 4); } return $phone; // Return original if not 10 digits }