使用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):
I need to replace all Phone NO prefix (0121 or 121) with new number prefix (072 or 72):
$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
I tried to use PREG_REPLACE But i have problem with 8401219101219, number change to 84072910729. It should be 840729101219
如何使用PHP更新所有电话号码
How should I update all Phone NO using PHP
推荐答案
此代码将执行您想要的操作.我假设您实际上要用072
或72
替换0121
或121
,因为这是示例数据显示的内容.如果您确实要替换122
,只需在下面的正则表达式中将121
更改为122
:
This code will do what you want. I'm presuming you actually want to replace 0121
or 121
with 072
or 72
since that's what your sample data shows. If you really want to replace 122
, just change 121
to 122
in the regex below:
$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
)
这篇关于使用php替换多种格式的电话前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!