dpwtr666638 2019-04-08 14:49
浏览 122
已采纳

如何使用PHP格式化数字到塞尔维亚格式?

I have number sent from form like this:

0641234567
064-123/4567
064/123-4567
3816412345678

And it needs to be like this:

+3816412345678

with +, without 0 and with max 14 characters including '+'.

How can i solve that using regex?

  • 写回答

2条回答 默认 最新

  • doutuo7609 2019-04-08 15:09
    关注

    All you need is multiple replacement with basically three rules,

    • replace - or / with empty string
    • replace the zero in the beginning with +381
    • Put a + in the beginning of number if the first number is any 1 to 9

    Check this PHP Demo,

    $arr = ['0641234567','064-123/4567','064/123-4567','3816412345678'];
    
    foreach($arr as $s) {
        echo $s." --> ".preg_replace(['/^0/', '/^(?=[1-9])/', '/[-\/]/'], ['+381', '+', ''], $s)."
    ";
    }
    

    Prints,

    0641234567 --> +381641234567
    064-123/4567 --> +381641234567
    064/123-4567 --> +381641234567
    3816412345678 --> +3816412345678
    

    Let me know if any of your case goes uncovered.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥50 成都蓉城足球俱乐部小程序抢票