Rails number_to_phone:从电话号码中删除国家代码

时间:2021-12-16 06:28:56

I have an application that stores phone numbers as '+11231231234'. To make it easier for the user, I convert it to: +1(123) 123-1234 in the view. I use the number_to_phone helper in rails to do it:

我有一个将电话号码存储为“+11231231234”的应用程序。为了方便用户,我将其转换为:+1(123)123-1234。我在rails中使用number_to_phone helper:

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

I also want to remove the +1 from the view. What would I need to write to remove the first two characters of the phone number if the first two characters are +1?

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

2 个解决方案

#1


7  

number_to_phone(call.From, :area_code => true).gsub(/^\+\d/, '')

#2


2  

Another way:

另一种方法:

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

Of course this is valid only if you always want to remove the first two characters but it's easier on the cpu :)

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

#1


7  

number_to_phone(call.From, :area_code => true).gsub(/^\+\d/, '')

#2


2  

Another way:

另一种方法:

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

Of course this is valid only if you always want to remove the first two characters but it's easier on the cpu :)

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