Create Random Safaricom Kenya Phone Numbers using Faker regexify
Faker is an amazing PHP library you can use to generate fake data, e.g names, addresses, etc.
One of it’s amazing features is the ability to create random data based on a regular expression. Using the regexify
function, you can create a random value that satisfies a given regular expression.
A sample use case is generating test phone numbers based on a phone number regular expression. In my case, I have some test cases that require me to generate a Safaricom Kenya mobile number.
The regex I’m using for the phone number is: /(\+?254|0){1}[7]{1}([0-2]{1}[0-9]{1}|[9]{1}[0-2]{1})[0-9]{6}/
. That matches the following:
- Phone numbers starting with ‘0’ (national format), or ‘254’ (international format)
- Followed by ‘7[0-2][0-9]’ or ‘7[9][0-2]’, e.g. 701, 711, 712, 790, 791, 792, etc
- Followed by 6 digits in the range of 0-9
NB: For phone number validation purposes, the regex could be made more forgiving, e.g. by allowing a space between the ‘254’ and the rest of the number, etc
Here’s an example of how I’m using it:
In this case, a random valid valid Safaricom Kenya phone number is generated every time you use the function. Feel free to use it in your code!
I’ve created a gist with the pure PHP code here
PS: I have a list of regex you can use to validate Kenyan phone numbers here, feel free to use that too. Additions/modifications are welcome ;-)