PHPackages                             newway-solutions/laravel-phone-number-formatter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. newway-solutions/laravel-phone-number-formatter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

newway-solutions/laravel-phone-number-formatter
===============================================

Phone number formatter for Myanmar and international format

v1.0.2(5mo ago)00PHPPHP &gt;=8.1

Since Dec 9Pushed 5mo agoCompare

[ Source](https://github.com/phonehtut/laravel-phone-number-formatter)[ Packagist](https://packagist.org/packages/newway-solutions/laravel-phone-number-formatter)[ RSS](/packages/newway-solutions-laravel-phone-number-formatter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Laravel Phone Number Formatter
==============================

[](#laravel-phone-number-formatter)

A Laravel package for formatting and validating phone numbers with support for Myanmar and international formats.

Features
--------

[](#features)

- 📱 Format phone numbers to international format (E.164)
- ✅ Validate phone numbers by country
- 🌍 Support for multiple countries (Myanmar, US, UK, India, China, Thailand)
- 🎯 Easy-to-use facade and service container integration
- ⚙️ Configurable default country
- 🧪 Fully tested with Pest PHP

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Laravel &gt;= 10.0

Installation
------------

[](#installation)

You can install the package via Composer:

```
composer require newway-solutions/laravel-phone-number-formatter
```

The package will automatically register its service provider and facade.

Configuration
-------------

[](#configuration)

Publish the configuration file (optional):

```
php artisan vendor:publish --tag=config --provider="NewwaySo\PhoneNumberFormatter\PhoneNumberFormatterServiceProvider"
```

This will create a `config/phoneformatter.php` file where you can set the default country:

```
return [
    'default_country' => 'MY', // Myanmar
];
```

Usage
-----

[](#usage)

### Using the Facade

[](#using-the-facade)

```
use NewwaySo\PhoneNumberFormatter\Facades\PhoneFormatter;

// Format a Myanmar phone number
$formatted = PhoneFormatter::format('0912345678', 'MY');
// Returns: +95912345678

// Format a US phone number
$formatted = PhoneFormatter::format('2025550123', 'US');
// Returns: +12025550123

// Validate a phone number
$isValid = PhoneFormatter::validate('0912345678', 'MY');
// Returns: true
```

### Using Dependency Injection

[](#using-dependency-injection)

```
use NewwaySo\PhoneNumberFormatter\PhoneNumberFormatter;

class YourController extends Controller
{
    public function __construct(
        protected PhoneNumberFormatter $formatter
    ) {}

    public function formatPhone($number)
    {
        return $this->formatter->format($number, 'MY');
    }
}
```

### Using the Service Container

[](#using-the-service-container)

```
$formatter = app('phoneformatter');
$formatted = $formatter->format('0912345678', 'MY');
```

### Direct Instantiation

[](#direct-instantiation)

```
use NewwaySo\PhoneNumberFormatter\PhoneNumberFormatter;

$formatter = new PhoneNumberFormatter();
$formatted = $formatter->format('0912345678', 'MY');
```

Supported Countries
-------------------

[](#supported-countries)

The package currently supports the following countries:

Country CodeCountryPrefixLocal PrefixLengthMYMyanmar+95098USUnited States+1-10UKUnited Kingdom+44-10INIndia+91-10CNChina+86-11THThailand+66-9API Reference
-------------

[](#api-reference)

### `format(string $number, string $country = 'MY'): string`

[](#formatstring-number-string-country--my-string)

Formats a phone number to international format (E.164).

**Parameters:**

- `$number` (string): The phone number to format (can include spaces, dashes, parentheses)
- `$country` (string): Two-letter country code (default: 'MY')

**Returns:** Formatted phone number with country prefix (e.g., `+95912345678`)

**Example:**

```
PhoneFormatter::format('0912345678', 'MY'); // +95912345678
PhoneFormatter::format('(202) 555-0123', 'US'); // +12025550123
PhoneFormatter::format('0912345678'); // +95912345678 (uses default MY)
```

**Notes:**

- All non-numeric characters are automatically removed
- For Myanmar numbers, the local prefix `09` is automatically removed if present
- If the country is not supported, returns the cleaned number without formatting

### `validate(string $number, string $country = 'MY'): bool`

[](#validatestring-number-string-country--my-bool)

Validates a phone number against the expected length for the specified country.

**Parameters:**

- `$number` (string): The phone number to validate
- `$country` (string): Two-letter country code (default: 'MY')

**Returns:** `true` if valid, `false` otherwise

**Example:**

```
PhoneFormatter::validate('0912345678', 'MY'); // true
PhoneFormatter::validate('2025550123', 'US'); // true
PhoneFormatter::validate('12345', 'MY'); // false (too short)
```

**Notes:**

- All non-numeric characters are automatically removed before validation
- Validation checks the length of the number after removing local prefixes
- Returns `false` if the country is not supported

Examples
--------

[](#examples)

### Formatting Myanmar Phone Numbers

[](#formatting-myanmar-phone-numbers)

```
use NewwaySo\PhoneNumberFormatter\Facades\PhoneFormatter;

// Various input formats
PhoneFormatter::format('0912345678', 'MY');     // +95912345678
PhoneFormatter::format('912345678', 'MY');       // +95912345678
PhoneFormatter::format('09-123-456-78', 'MY');   // +95912345678
PhoneFormatter::format('(09) 1234 5678', 'MY'); // +95912345678
```

### Formatting International Numbers

[](#formatting-international-numbers)

```
// United States
PhoneFormatter::format('2025550123', 'US');      // +12025550123
PhoneFormatter::format('(202) 555-0123', 'US');  // +12025550123

// United Kingdom
PhoneFormatter::format('7911123456', 'UK');      // +447911123456

// India
PhoneFormatter::format('9876543210', 'IN');      // +919876543210

// China
PhoneFormatter::format('13800138000', 'CN');     // +8613800138000

// Thailand
PhoneFormatter::format('812345678', 'TH');       // +66812345678
```

### Validation Examples

[](#validation-examples)

```
// Valid Myanmar number
PhoneFormatter::validate('0912345678', 'MY'); // true

// Invalid Myanmar number (wrong length)
PhoneFormatter::validate('1234567', 'MY'); // false

// Valid US number
PhoneFormatter::validate('2025550123', 'US'); // true

// Invalid US number (wrong length)
PhoneFormatter::validate('202555012', 'US'); // false
```

### Using in Laravel Forms

[](#using-in-laravel-forms)

```
// In a FormRequest
use NewwaySo\PhoneNumberFormatter\Facades\PhoneFormatter;

public function rules()
{
    return [
        'phone' => [
            'required',
            function ($attribute, $value, $fail) {
                if (!PhoneFormatter::validate($value, 'MY')) {
                    $fail('The phone number is invalid.');
                }
            },
        ],
    ];
}

public function prepareForValidation()
{
    $this->merge([
        'phone' => PhoneFormatter::format($this->phone, 'MY'),
    ]);
}
```

### Using in Models

[](#using-in-models)

```
use NewwaySo\PhoneNumberFormatter\Facades\PhoneFormatter;

class User extends Model
{
    public function setPhoneAttribute($value)
    {
        $this->attributes['phone'] = PhoneFormatter::format($value, 'MY');
    }

    public function getFormattedPhoneAttribute()
    {
        return PhoneFormatter::format($this->phone, 'MY');
    }
}
```

Testing
-------

[](#testing)

Run the tests with:

```
composer test
```

Or using Pest directly:

```
./vendor/bin/pest
```

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

Adding Support for New Countries
--------------------------------

[](#adding-support-for-new-countries)

To add support for a new country, you can extend the `PhoneNumberFormatter` class or modify the `$countries` array:

```
protected array $countries = [
    'MY' => ['prefix' => '+95', 'local_prefix' => '09', 'length' => 8],
    'US' => ['prefix' => '+1', 'length' => 10],
    // Add your country here
    'SG' => ['prefix' => '+65', 'length' => 8],
];
```

Security
--------

[](#security)

If you discover any security-related issues, please email  instead of using the issue tracker.

Changelog
---------

[](#changelog)

Please see [CHANGELOG.md](CHANGELOG.md) for more information on what has changed recently.

License
-------

[](#license)

The Laravel Phone Number Formatter package is open-sourced software licensed under the [MIT license](LICENSE.md).

Author
------

[](#author)

**Phone Htut Khaung**

- CTO at Newway Solutions
- Website:
- Email:

Support
-------

[](#support)

For support, please open an issue on the [GitHub repository](https://github.com/phonehtut/laravel-phone-number-formatter) or contact the maintainer.

Credits
-------

[](#credits)

- Built with ❤️ for the Laravel community
- Special thanks to all contributors

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance73

Regular maintenance activity

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

154d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bc8b15b92bff167f4e2a4422fb3ba07e22b7e15b2cf07786ce68f1fad4c7c2b7?d=identicon)[phonehtut](/maintainers/phonehtut)

![](https://www.gravatar.com/avatar/2b91b687216539bac2e24f1e0d874c2570f43262a6c977f4b3f18f5e53bd8b42?d=identicon)[newway-solutions](/maintainers/newway-solutions)

---

Top Contributors

[![phonehtut](https://avatars.githubusercontent.com/u/163448120?v=4)](https://github.com/phonehtut "phonehtut (6 commits)")

### Embed Badge

![Health badge](/badges/newway-solutions-laravel-phone-number-formatter/health.svg)

```
[![Health](https://phpackages.com/badges/newway-solutions-laravel-phone-number-formatter/health.svg)](https://phpackages.com/packages/newway-solutions-laravel-phone-number-formatter)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
