skip to Main Content

I’m currently struggling with a PHP issue : I’d like to use the str_starts_with function (AFAIK available from PHP 8.0) to check if a given phone number actually starts with a phone code (all the phone codes are defined in an array).

According to documentation, it seems the str_starts_with function doesn’t support arrays as parameter, that’s why I’m trying to find another way to solve my problem.

I used the foreach loop to check all the codes one-by-one, and see if the given phone number actually starts with one of the codes contained in the array. But it seems that combining foreach, array and str_starts_with is not always possible.

The greatest advantage of the str_starts_with function is that it can handle codes of various lengths : for example, there are phone codes with one digit (+1 : Canada, U. S. A., and many other), two digits (+33 : France), three digits (+353 : Ireland).

Here is what I tried :

$tel = '+33601020304';

$tel = preg_replace('/[^0-9]/', '', $tel); //33601020304

$tel = ltrim($tel, '0'); //33601020304

$tel_array = array('32' => 'Belgique', '33' => 'France', '34' => 'Espagne', '41' => 'Suisse', '44' => 'Royaume-Uni', '49' => 'Allemagne');

foreach($tel_array as $pays => $code)
{
   str_starts_with($tel, $pays);
   echo $pays.'_'.$code.'<br />';
}

If the number starts with one of the codes from the array, I’d like to return the country where the number comes from (name of the country also contained in the array). Else, I would like to return an error.

Instead of that, for the moment, I get this :

32_Belgique
33_France
34_Espagne
41_Suisse
44_Royaume-Uni
49_Allemagne

How could I get and display the country to which the number is assigned, and display an error else ?

2

Answers


  1. str_starts_with returns a bool. You need to use this in an if statement. So if str_starts_with, then echo the string.

    Login or Signup to reply.
  2. It is not best practice to iterate each element in your lookup array; therefore str_starts_with() is also not recommended.

    You need to isolate the first two characters from the sanitized $tel string before trying to reference the lookup array. Checking for the existence of a given key will always outperform iterating the array or making value comparisons.

    Code: (Demo)

    $tel = '+33601020304';
    
    $tel = ltrim(preg_replace('/D+/', '', $tel)); //33601020304
    
    $tel_array = [
        '32' => 'Belgique',
        '33' => 'France',
        '34' => 'Espagne',
        '41' => 'Suisse',
        '44' => 'Royaume-Uni',
        '49' => 'Allemagne'
    ];
    
    echo $tel_array[substr($tel, 0, 2)] ?? 'not found';
    

    If your actual project requirements are more nuanced, then it may be a better idea to rely on a comprehensive library that was built to handle phone number operations. "LibPhoneNumber" You can find many posts on this topic via https://stackoverflow.com/search?q=libphonenumber+%5Bphp%5D.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search