PHP code example of piyo2 / format-phone-jp

1. Go to this page and download the library: Download piyo2/format-phone-jp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

piyo2 / format-phone-jp example snippets


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

$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'

$f = new PhoneJP();

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

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