英文:
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 = '+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 ?
答案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
'353' => 'Ireland',
// 2 digit
'32' => 'Belgique', '33' => 'France',
'34' => 'Espagne', '41' => 'Suisse', '44' => 'Royaume-Uni',
'49' => 'Allemagne',
// 1 digit
'1' => 'USA',
];
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 => $code) {
if (str_starts_with($tel, $pays)) {
echo $pays . '_' . $code . '<br />';
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论