从电话号码中删除国家/地区代码?

时间:2018-08-09 08:24:47

标签: php preg-replace

下面是我目前用于格式化数据库电话号码的代码。但是,iOS会在自动填充之前添加​​+1或1(国家代码)。如何删除电话号码前的1?

所以...

+12223334444应该是2223334444

12223334444应该是2223334444

using System.Linq;
using System.Threading.Tasks;
using SharpCaster.Controllers;
using SharpCaster.Services;
using Xunit;

namespace SharpCaster.Test
{
    public class WebPageCastingTester
    {
        private ChromecastService _chromecastService;
        public WebPageCastingTester()
        {
            _chromecastService = ChromecastService.Current;
            var device = _chromecastService.StartLocatingDevices().Result;
            _chromecastService.ConnectToChromecast(device.First()).Wait(2000);
        }

        [Fact]
        public async void TestingLaunchingSharpCasterDemo()
        {
            var controller =  await _chromecastService.ChromeCastClient.LaunchWeb();
            await Task.Delay(4000);
            Assert.NotNull(_chromecastService.ChromeCastClient.ChromecastStatus.Applications.First(x => x.AppId == WebController.WebAppId));
            await controller.LoadUrl("https://www.windytv.com/");
            await Task.Delay(4000);
            Assert.Equal(_chromecastService.ChromeCastClient.ChromecastStatus.Applications.First(x => x.AppId == WebController.WebAppId).StatusText,
                "Now Playing: https://www.windytv.com/");
        }
    }
}

4 个答案:

答案 0 :(得分:2)

使用以下正则表达式替换:

如果您的格式是:+12223334444

$country_code = '+1';
$phone_no = '+12223334444';

echo preg_replace('/^\+?1|\|1|\D/', '', ($phone_no));

输出将为2223334444

如果您的格式是:12223334444

$country_code = '1';
$phone_no = '2223334444';

echo preg_replace('/^\+?1|\|1|\D/', '', ($phone_no));

输出将为2223334444

答案 1 :(得分:0)

这可能是矫kill过正,但是我们开始:

\+(?:998|996|995|994|993|992|977|976|975|974|973|972|971|970|968|967|966|965|964|963|962|961|960|886|880|856|855|853|852|850|692|691|690|689|688|687|686|685|683|682|681|680|679|678|677|676|675|674|673|672|670|599|598|597|595|593|592|591|590|509|508|507|506|505|504|503|502|501|500|423|421|420|389|387|386|385|383|382|381|380|379|378|377|376|375|374|373|372|371|370|359|358|357|356|355|354|353|352|351|350|299|298|297|291|290|269|268|267|266|265|264|263|262|261|260|258|257|256|255|254|253|252|251|250|249|248|246|245|244|243|242|241|240|239|238|237|236|235|234|233|232|231|230|229|228|227|226|225|224|223|222|221|220|218|216|213|212|211|98|95|94|93|92|91|90|86|84|82|81|66|65|64|63|62|61|60|58|57|56|55|54|53|52|51|49|48|47|46|45|44\D?1624|44\D?1534|44\D?1481|44|43|41|40|39|36|34|33|32|31|30|27|20|7|1\D?939|1\D?876|1\D?869|1\D?868|1\D?849|1\D?829|1\D?809|1\D?787|1\D?784|1\D?767|1\D?758|1\D?721|1\D?684|1\D?671|1\D?670|1\D?664|1\D?649|1\D?473|1\D?441|1\D?345|1\D?340|1\D?284|1\D?268|1\D?264|1\D?246|1\D?242|1)\D?

Demo

为了匹配任何前缀,并且由于某些国家/地区可能共享部分相同的前缀(例如1和1-721),请按降序和大小将所有正则表达式添加到正则表达式中。

正则表达式基于this list

答案 2 :(得分:0)

 if (substr($request->mobile_num, 0, 2) == "98")
        $mobile_num = str_replace('98', '0', $request->mobile_num);
 elseif (substr($request->mobile_num, 0, 3) == "+98")
        $mobile_num = str_replace('+98', '0', $request->mobile_num);
 else
     $mobile_num = $request->mobile_num;

答案 3 :(得分:0)

它会清理电话号码并去除(,),-,+ 仅返回最后10位数字的电话号码

示例:

1234567890 => +11234567890
1234567890 => :+112:34567890
1234567890 => Phone :+1-123-456-7890
1234567890 => Phone :+1-(123)-(456)-(7890)
1234567890 => +1-(123)-(456)-(7890)
1234567890 => +1-123-456-7890

Sample test code:
$dirty_phone = ["+11234567890", ":+112:34567890", "Phone :+1-123-456-7890",  "Home :(+1)-(123)-(456)-(7890)", "+1-(123)-(456)-(7890)", "+1-123-456-7890"];

foreach ($dirty_phone as $key => $phone_no) {
    $cleaned_phone = clean_phone_number($phone_no);
    echo "{$cleaned_phone} => {$phone_no}<br>";
}

function clean_phone_number($phone_no){
    return substr( preg_replace('/^\+?1|\|1|\D/', '', $phone_no), -10);
}