Faker generating strange phone numbers?

Viewed 25401

So I'm creating a database with Laravel and seeding it, and I ran into an issue where it generates a phone number as follows: (635) 889-5802 x45134. This causes issues by exceeding the desired length of the database column.

So my questions are:

  1. On a practical level, why does it do this? What is the real-world use case of x#####? I'm looking around and can't find a good explanation, best guess is an internal organization extension.
  2. When building an application where internationalization might eventually be important, is it a good idea to build in support for whatever these are? If so, then expanding the column width would be a better solution for me.
  3. If this isn't important, is there a way to prevent these x##### from being attached to the number on generation? I feel like this should be an option. If not it sounds like my best options would be a custom provider or a regex filter to remove these values.
4 Answers

You need to ensure faker is using the correct locale. Try this:

use Faker\Factory as Faker;

//...

$faker = Faker::create('en_GB');

Faker will then generate GB compatible phone numbers etc

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,

To answer your Question 3:

If you cannot or do not want to change the faker generation of phone numbers, you can write them to a file and filter out the unwanted extensions afterwards.

Based on @CharlesDuffy's answer to this question, the following will work in a bash shell:

$ while read -r; do echo "${REPLY%%x*}"; done <numbers >extensionsremoved

where 'numbers' is the name of your file containing lines ending with a fake phone number.

The assumption here is that 'x' always and only occurs in front of an extension at the end of a line. I tested it with 1000 fake numbers generated by the Python Faker, and it worked.

Related