File Manager
Editing: functions.php
<?php // functions.php /** * Formats and validates a phone number. * * @param string $phoneNumber The phone number to format. * @param string $countryCode The country code (e.g., +1, +44, +255). * @return string|false The formatted phone number or false if invalid. */ function formatPhoneNumber($phoneNumber, $countryCode) { // Ensure the country code starts with a '+' if (strpos($countryCode, '+') !== 0) { $countryCode = '+' . $countryCode; } // Remove any non-numeric characters from the phone number $phoneNumber = preg_replace('/[^0-9]/', '', $phoneNumber); // Remove leading zero if it exists if (strpos($phoneNumber, '0') === 0) { $phoneNumber = substr($phoneNumber, 1); } // Add country code if it's missing if (strpos($phoneNumber, $countryCode) !== 0) { $phoneNumber = $countryCode . $phoneNumber; } // Validate the phone number format if (!preg_match('/^\+\d{6,15}$/', $phoneNumber)) { return false; // Invalid format } return $phoneNumber; }
💾 Save
⬅ Back