繁体   English   中英

使用php替换多种格式的电话前缀

[英]Replace phone prefix of multiple format using php

我有一个数组:

    $phone_number = array ( 'phone' => '01219104579', 
    'phone' => '01219104579@abc.
    'phone' => '+8401219101219',
    'phone' => '01219104579/01219104479',
    'phone' => '841219104579@abc.com',
    'phone' => 'abcd01219104579@abc.com',
    'phone' => 'Hồ2101219104579@abc.com'
);

我需要用新号码前缀(072 或 72)替换所有电话号码前缀(0121 或 121):

$phone_number = array ( 'phone' => '0729104579', 
'phone' => '0729104579@abc.com', 
'phone' => '+840729101219', 
'phone' => '0729104579/0729104479', 
'phone' => '84729104579@abc.com',
'phone' => 'abcd0729104579@abc.com',
'phone' => 'Hồ210729104579@abc.com' ); 

我尝试使用 PREG_REPLACE 但我遇到了 8401219101219 的问题,编号更改为 84072910729。应该是 840729101219

我应该如何使用 PHP 更新所有电话号码

此代码将执行您想要的操作。 我假设您实际上想用07272替换0121121 ,因为这就是您的示例数据显示的内容。 如果你真的想替换122 ,只需在下面的正则表达式中将121更改为122

$phone_numbers = array ('01219104579', 
'01219104579@abc.com',
'+8401219101219',
'01219104579/01219104479',
'841219104579@abc.com'
);

foreach ($phone_numbers as $phone_number) {
    $new_numbers[] = preg_replace('/\b(\+?84?0?|0)121/', '${1}72', $phone_number);
}
print_r($new_numbers);

输出:

Array
(
    [0] => 0729104579
    [1] => 0729104579@abc.com
    [2] => +840729101219
    [3] => 0729104579/0729104479
    [4] => 84729104579@abc.com
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM