You can set custom format on the fly like this:
Faker::Base.numerify('+90(###) ### ####')
This will solve your problem.
Answer from zeitnot on Stack OverflowYou can set custom format on the fly like this:
Faker::Base.numerify('+90(###) ### ####')
This will solve your problem.
Faker::PhoneNumber.cell_phone is basically just calling numerify with one of the predefined phone_number_formats.
So you could just use numerify with your own format. For e.g. If you want 10 digits number, you would do:
Faker.numerify('#########')
If you'd still like to use Faker::PhoneNumber.cell_phone but would like to get rid of the hyphens, you could use gsub to replace the hyphens as:
Faker::PhoneNumber.cell_phone.gsub(/-/, '')
Unfortunately, Faker doesn't have a built-in function to do this.
You have two options.
Write functions yourself, for example:
def fake_phone_number(fake: Faker) -> str:
return f'+91 {fake.msisdn()[3:]}'
from faker import Faker
def main():
fake = Faker()
print(fake_phone_number(fake))
Or create an appropriate provider and suggest adding it: https://faker.readthedocs.io/en/master/communityproviders.html
Python src MSISDN. What is MSISDN.
Or
from faker import Faker
from faker.providers.phone_number import Provider
class IndiaPhoneNumberProvider(Provider):
"""
A Provider for phone number.
"""
def india_phone_number(self):
return f'+91 {self.msisdn()[3:]}'
def main():
fake = Faker()
fake.add_provider(IndiaPhoneNumberProvider)
print(fake.india_phone_number())
You can use faker's hindi Indian provider, it will generate most of the numbers with +91, but not all:
Code sample with faker and factoryboy.Faker:
import factory
from faker import Faker
fake = Faker(locale="hi_IN")
fake.phone_number()
# with factory-boy's faker
class New(factory.DictFactory):
phone = factory.Faker("phone_number", locale="hi_IN")
New()
Usage result:
print(fake.phone_number())
>> +91 9018742635
print(New())
>> {'phone': '+91 1489998444'}
If you want to ensure that every number has a +91, you need to tweak with factoryboy or wrap fake.phone_number with a parsing function.
A neat solution could be using a library called phonenumbers like this:
import phonenumbers
from faker import Faker
fake = Faker(locale="hi_IN")
number = fake.phone_number()
num_obj = phonenumbers.parse(number, "IN")
interanational_IN_number = phonenumbers.format_number(num_obj, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
print(interanational_IN_number)
This should work most of the time, except with numbers starting with 091 - they should have two leading zero to be identified properly (0091), fix that corner case, and you should be good to go.
x#### is commonly used to indicate a phone extension. From looking at the source code for the phone number provider, there's no option to remove the extension so you'd have to do so yourself.
However, you may want to consider reading Falsehoods Programmers Believe About Phone Numbers.
To avoid the problem your migration should be like this..
$table->string('phone_number');
And your seeder should be like this.
'phone_number' => $faker->phoneNumber,