piyo2/format-phone-jp

Japanese phone number validator/formatter.

v2.0.0 2022-10-18 03:20 UTC

This package is auto-updated.

Last update: 2024-12-27 06:10:14 UTC


README

Japanese phone number validator/formatter.

Usage

Basic

use piyo2\format\PhoneJP;

$f = new PhoneJP();

// Valid number
$f->validate('0120444444'); // => true
$f->format('0120444444'); // => '0120-444-444'
$f->formatIfValid('0120444444'); // => '0120-444-444'

// Invalid number
$f->validate('0127-12-3456'); // => false
$f->format('0127-12-3456'); // => '0127-12-3456' (= input value)
$f->formatIfValid('0127-12-3456'); // => null

Force prefix

$f = new PhoneJP();

$f->format('120444444'); // => '120-444-444'
$f->format('0120444444'); // => '0120-444-444'
$f->format('+81120444444'); // => '+81 120-444-444'

$f->setPrefixMode(PhoneJP::PREFIX_FORCE_DOMESTIC);
$f->format('120444444'); // => '0120-444-444'

$f->setPrefixMode(PhoneJP::PREFIX_FORCE_COUNTRY);
$f->format('120444444'); // => '+81 120-444-444'

Set delimiter

$f = new PhoneJP();

$f->setDelimiter(' ');
$f->format('0120444444'); // => '0120 444 444'

$f->setDelimiter('-');
$f->setCountryPrefixDelimiter('-');
$f->format('+81120444444'); // => '+81-120-444-444'

Data source