Using str_starts_with to check if a number starts with a given phone code (using array and foreach)

huangapple go评论67阅读模式
英文:

Using str_starts_with to check if a number starts with a given phone code (using array and foreach)

问题

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 ?

英文:

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 = &#39;+33601020304&#39;;

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

$tel = ltrim($tel, &#39;0&#39;); //33601020304

$tel_array = array(&#39;32&#39; =&gt; &#39;Belgique&#39;, &#39;33&#39; =&gt; &#39;France&#39;, &#39;34&#39; =&gt; &#39;Espagne&#39;, &#39;41&#39; =&gt; &#39;Suisse&#39;, &#39;44&#39; =&gt; &#39;Royaume-Uni&#39;, &#39;49&#39; =&gt; &#39;Allemagne&#39;);

foreach($tel_array as $pays =&gt; $code)
{
   str_starts_with($tel, $pays);
   echo $pays.&#39;_&#39;.$code.&#39;&lt;br /&gt;&#39;;
}

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 ?

答案1

得分: 1

如果您按最长的代码先排序您的代码列表,这将确保您首先获得最长可能的匹配。

因此,使用您问题中的示例...

$tel_array = [
    // 3 位数字
    '353' => '爱尔兰',
    // 2 位数字
    '32' => '比利时', '33' => '法国',
    '34' => '西班牙', '41' => '瑞士', '44' => '英国',
    '49' => '德国',
    // 1 位数字
    '1' => '美国',
];

然后遍历它们,一旦找到匹配(`str_starts_with` 返回 true),就输出匹配并停止查找...

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

If you organise your list of codes with the longest codes first, this will ensure that you take the longest possible match first.

So using the examples from your question...

$tel_array = [
    // 3 digit
    &#39;353&#39; =&gt; &#39;Ireland&#39;,
    // 2 digit
    &#39;32&#39; =&gt; &#39;Belgique&#39;, &#39;33&#39; =&gt; &#39;France&#39;,
    &#39;34&#39; =&gt; &#39;Espagne&#39;, &#39;41&#39; =&gt; &#39;Suisse&#39;, &#39;44&#39; =&gt; &#39;Royaume-Uni&#39;,
    &#39;49&#39; =&gt; &#39;Allemagne&#39;,
    // 1 digit
    &#39;1&#39; =&gt; &#39;USA&#39;,
];

Then loop through them and once you find a match (str_starts_with returns true), the echo out the match and stop looking...

foreach ($tel_array as $pays =&gt; $code) {
    if (str_starts_with($tel, $pays)) {
        echo $pays . &#39;_&#39; . $code . &#39;&lt;br /&gt;&#39;;
        break;
    }
}

答案2

得分: 0

str_starts_with 返回一个布尔值。您需要在一个 if 语句中使用它。所以如果 str_starts_with 为真,那么输出字符串。

英文:

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

huangapple
  • 本文由 发表于 2023年6月1日 05:03:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定