PHPackages                             gmarsano/chilean-phone-tool - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. gmarsano/chilean-phone-tool

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

gmarsano/chilean-phone-tool
===========================

validate and format chilean phone numbers.

v1.0.2(4y ago)015MITPHPPHP &gt;=7.3

Since Nov 13Pushed 4y ago1 watchersCompare

[ Source](https://github.com/gmarsano/chilean-phone-tool)[ Packagist](https://packagist.org/packages/gmarsano/chilean-phone-tool)[ RSS](/packages/gmarsano-chilean-phone-tool/feed)WikiDiscussions master Synced 1mo ago

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

🇨🇱 Chilean Phone Tool ☎️
========================

[](#-chilean-phone-tool-️)

Introduction
------------

[](#introduction)

A PHP composer package to work with Chilean 🇨🇱 phone numbers.

### Features

[](#features)

- Parsing
- Get prefix
- Validation
- Formatting
- Factory

### Contents

[](#contents)

- [Introduction](#introduction)
    - [Features](#features)
    - [Contents](#contents)
- [Install](#install)
    - [Requirements](#requirements)
- [Usage](#usage)
    - [Set a phone number.](#set-a-phone-number)
        - [`parse`](#parse)
        - [`setPhone`](#setphone)
    - [Validation](#validation)
        - [`validate`](#validate)
        - [`errors` and `messages`](#errors-and-messages)
            - [Error messages](#error-messages)
        - [Ignore prefix validation](#ignore-prefix-validation)
        - [`fix` and `luckyFix`](#fix-and-luckyfix)
    - [Formatting](#formatting)
        - [`format`](#format)
        - [Choosing format](#choosing-format)
    - [Factory](#factory)
    - [Aliases](#aliases)
- [Inspired by](#inspired-by)
- [License](#license)

Install
-------

[](#install)

```
composer require gmarsano/chilean-phone-tool
```

### Requirements

[](#requirements)

- PHP &gt;= 7.3

Usage
-----

[](#usage)

```
use Gmarsano\ChileanPhoneTool\Phone;

$phone = Phone::parse("+56 9 87-654-321");
$phone->validate();   // true
$phone->number();     // "987654321"
$phone->fullNumber(); // "56987654321"
$phone->prefix()      // "9"
$phone->format();     // "+56 9 87-654-321"
```

### Set a phone number.

[](#set-a-phone-number)

#### `parse`

[](#parse)

Usually you may want to take a phone in any format, check if it's valid and bring it to the desired format. Use `parse` to set the number, if that the case.

If you parse a valid number you can get the number and prefix just calling these:

```
Phone::parse("+56 9 87-654-321")->number();
// => "987654321"

Phone::parse("+56-32-7-654-321")->prefix();
// => "32"
```

Clean an invalid input (use `quiet` to prevent validation exceptions):

```
Phone::parse("2 (09) 987654321")->quiet()->get();
// => "209987654321"

// yes work with integer or float, too.
Phone::parse(1.23)->quiet()->number();
// => "123"
```

#### `setPhone`

[](#setphone)

Sometimes you just want to know if an input value is a valid phone number without clean or make any changes on it before validation. It's time to use `setPhone`:

```
Phone::setPhone("56987654321")->quiet()->validate();
// => true

Phone::setPhone("987654321")->quiet()->validate();
// => true

Phone::setPhone("+56 9 87-654-321")->quiet()->validate();
// => false
```

### Validation

[](#validation)

#### `validate`

[](#validate)

Use `validate` to check if it is a valid number. An invalid number will throw an exception with a message giving information about the reasons.

```
Phone::parse("+56-32-7-654-321")->validate();
// => true

Phone::parse("+56 1 87-654-321")->validate();
// Exception with message 'Invalid prefix.'
```

The features of this tool make sense on a valid phone. This is why by default it works with exceptions. But you can use `quiet` to avoid this behavior, but be careful, if you try to get or format an invalid number you will get the original digits or value depending on the case.

#### `errors` and `messages`

[](#errors-and-messages)

They allow to obtain an array with validation messages if `quiet` has been used. They may look like aliases, but there is a little difference:

- with `errors` you get a list of the validation messages with error codes as keys.
- with `messages` you only get the messages.

```
$phone = Phone::parse("+56 1 87-654-321");
$phone->quiet()->validate();
// => false

$phone->errors()
/*
=> [
     5 => "Invalid prefix.",
   ]
*/

$phone->messages()
/*
=> [
     "Invalid prefix.",
   ]
*/
```

##### Error messages

[](#error-messages)

codemessage

2Empty digits count.3Invalid phone number format.4Invalid country code.5Invalid prefix.6Invalid phone number.#### Ignore prefix validation

[](#ignore-prefix-validation)

To avoid prefix validation use `ignorePrefix`:

```
$phone = Phone::parse("+56 1 87-654-321");
$phone->quiet()->validate();
// => false

$phone = Phone::parse("+56 1 87-654-321");
$phone->ignorePrefix()->validate();
// => true

$phone->get()
// => "187654321"

$phone->prefix()
// => "18"
```

#### `fix` and `luckyFix`

[](#fix-and-luckyfix)

Tries to set a valid phone from original input.

```
$phone = Phone::setPhone("+56 032 7-654-321");
$phone->quiet()->validate();
// => false

$phone->fix()->validate();
// => true

$phone->get();
// => "327654321"
```

Use `getOld` to get original value.

```
$phone->getOld();
// => "+56 032 7-654-321"
```

Sometimes the inputs may lack the Santiago prefix (2). Use `luckFix` to fix and try to guess if the missing prefix can be fixed.

```
$phone = Phone::parse("+56-37-654-321");
$phone->quiet()->validate();
// => false

$phone->luckyFix()->validate();
// => true

$phone->format();
// => "+56 2 37-654-321"

$phone->errors();
/*
=> [
     3 => "Invalid phone number format.",
   ]
*/
```

As you can see, after fix, validation can be true. If you need to check if original was modified because it was invalid, then you can count errors.

### Formatting

[](#formatting)

#### `format`

[](#format)

Use `format` on a valid phone to get the value in standard numbering format.

```
$phone = Phone::setPhone("987654321");
$phone->quiet()->validate();
// => true

$phone->format();
// => "+56 9 87-654-321"
```

#### Choosing format

[](#choosing-format)

Give `format` method the desired format as an argument.

format

example

FormatterInterface::STANDARD\_FORMAT+56 9 87-654-321, +56 75 7-654-321FormatterInterface::PREFIX\_FORMAT(9) 87-654-321, (75) 7-654-321FormatterInterface::DIGITS\_FORMAT56987654321, 56757654321FormatterInterface::NUMBER\_DIGITS\_FORMAT987654321, 757654321```
use Gmarsano\ChileanPhoneTool\Contracts\FormatterInterface;

Phone::setPhone("987654321")->quiet()
  ->format(FormatterInterface::PREFIX_FORMAT);
// => "(9) 87-654-321"
```

### Factory

[](#factory)

You can generate a valid phone number using Phone Tool:

```
Phone::factory()->make()->first();
// => "451036552"
```

Make multiple numbers giving a count as argument to `make` method.

```
Phone::factory()->make(5)->all();
/*
=> [
     "523677441",
     "243584227",
     "712584943",
     "671108073",
     "633943870",
   ]
*/
```

You can use the `unique`, `cellPhone`, `landLine` or `prefix` modifiers to make unique numbers, cell phone, landline (red fija), or set a valid prefix manually.

```
Phone::factory()->cellPhone()->make()->first();
// => "973986533"

Phone::factory()->unique()->landLine()->make(3)->all();
/*
=> [
     "649800571",
     "572435282",
     "805066069",
   ]
*/

Phone::factory()->unique()->prefix(2)->make(3)->all();
/*
=> [
     "249404634",
     "250034633",
     "243960969",
   ]
*/
```

Use `format` (check [choosing format](#choosing-format)) modifier to give desired format to numbers.

```
Phone::factory()->make()->format()->first();
// => "+56 68 4-987-466"

Phone::factory()->unique()->make(3)
  ->format(FormatterInterface::PREFIX_FORMAT)
  ->all();
/*
=> [
     "(9) 76-488-717",
     "(45) 1-806-739",
     "(42) 1-444-163",
   ]
*/

// try this if you quickly need digits with country code
Phone::factory()->make(1, true)->first();
// => "56555182350"
```

### Aliases

[](#aliases)

methodsaliasesget()numberget(true)fullNumbergetPrefixprefixvalidateisValidInspired by
-----------

[](#inspired-by)

[Freshworks Chilean Bundle](https://github.com/freshworkstudio/ChileanBundle)

License
-------

[](#license)

MIT License
Copyright (c) 2021 gmarsano

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

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 ~1 days

Total

5

Last Release

1637d ago

Major Versions

v0.1.1 → v1.02021-11-16

### Community

Maintainers

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

---

Tags

formattervalidatorphonechilechileantelefono

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gmarsano-chilean-phone-tool/health.svg)

```
[![Health](https://phpackages.com/badges/gmarsano-chilean-phone-tool/health.svg)](https://phpackages.com/packages/gmarsano-chilean-phone-tool)
```

###  Alternatives

[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M383](/packages/respect-validation)[seld/jsonlint

JSON Linter

1.3k217.8M205](/packages/seld-jsonlint)[composer/spdx-licenses

SPDX licenses list and validation library.

1.4k184.2M25](/packages/composer-spdx-licenses)[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M107](/packages/propaganistas-laravel-phone)[opis/json-schema

Json Schema Validator for PHP

64236.9M186](/packages/opis-json-schema)[laminas/laminas-validator

Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria

15544.9M188](/packages/laminas-laminas-validator)

PHPackages © 2026

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