I have a phone number in a database with country code in the prefix. For Eg: +919123456789. Now I need only last 10 digit mobile number without any prefix. How can i achieve that??
You can use String.slice(phone_number, -10, 10)
to get the last 10 letters of a string.
Thank you so much
There is a phone parser from @cevado
from docs, You can get phone info
iex> Phone.parse("555132345678")
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
That would extract national number…
Additionaly there is another package, ex_phone_number here, based on libphonenumber
Combining both, You can extract a2 code, phone string with phone, then parse it with ex_phone_number.
iex(6)> {:ok, phone_number} = ExPhoneNumber.parse "555132345678", "BR"
{:ok,
%ExPhoneNumber.Model.PhoneNumber{country_code: 55, country_code_source: nil,
extension: nil, italian_leading_zero: nil, national_number: 5132345678,
number_of_leading_zeros: nil, preferred_domestic_carrier_code: nil,
raw_input: nil}}
You get a phone structure and useful functions… From docs, You can
iex> ExPhoneNumber.is_possible_number?(phone_number)
true
iex> ExPhoneNumber.is_valid_number?(phone_number)
true
iex> ExPhoneNumber.get_number_type(phone_number)
:fixed_line
iex> ExPhoneNumber.format(phone_number, :national)
"044 668 18 00"
iex> ExPhoneNumber.format(phone_number, :international)
"+41 44 668 18 00"
iex> ExPhoneNumber.format(phone_number, :e164)
"+41446681800"
iex> ExPhoneNumber.format(phone_number, :rfc3966)
"tel:+41-44-668-18-00"
It is longer, but You can get full phone info
5 Likes