【问题标题】:Rails number_to_phone: Remove country code from phone numberRails number_to_phone:从电话号码中删除国家代码
【发布时间】:2012-10-22 19:36:53
【问题描述】:

我有一个将电话号码存储为'+11231231234' 的应用程序。为了方便用户,我在视图中将其转换为:+1(123) 123-1234。我使用 rails 中的 number_to_phone 助手来做到这一点:

<%= number_to_phone(call.From, :area_code => true) %>

我还想从视图中删除 +1。如果前两个字符是+1,我需要写什么来删除电话号码的前两个字符?

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:
    number_to_phone(call.From, :area_code => true).gsub(/^\+\d/, '')
    

    【讨论】:

    • 你就是凯尔!只要 stackoverflow 允许我,我就会接受答案
    • @StepanParunashvili 哈!谢谢。不急;)
    • 这是否处理像 +94 这样的国家代码,只是查看正则表达式,它只处理一位数字的国家代码,但最多可以有 3 个。
    【解决方案2】:

    另一种方式:

    number_to_phone(call.From, :area_code => true)[2..-1]
    

    当然,这仅在您总是想删除前两个字符时才有效,但在 cpu 上更容易:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      相关资源
      最近更新 更多