PHP code example of cse / helpers-phone

1. Go to this page and download the library: Download cse/helpers-phone 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/ */

    

cse / helpers-phone example snippets


switch (true) {
    case Phone::is($phone):
        break;
    case Phone::exist($phone):
        $phone = Phone::extract($phone);
        break;
    default:
        new Exception('Phone is not exist');
}

$phone = Phone::format($phone);

$numbers = [
    6677,
    4456677,
    '+1 233 44 566 77',
    '(233) 445-6677',
    '((233) 445-6677',
    2334456677,
    '233 445-6677',
    '233-445-6677',
    '(233)4456677',
    '+123344566-77',
    '1-2334456677',
    '233-445-66-77   -Hello!',
    '+1 - 2334456677'
];

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::clear($phone);
}
/**
* [
*     '6677',
*     '4456677',
*     '12334456677',
*     '2334456677',
*     '2334456677',
*     '2334456677',
*     '2334456677',
*     '2334456677',
*     '2334456677',
*     '12334456677',
*     '12334456677',
*     '2334456677',
*     '12334456677'
* ]
*/

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::format($phone);
}
/**
 * [
 *    '66-77',
 *    '445-66-77',
 *    '+1 (233) 445-66-77',
 *    '(233) 445-66-77',
 *    '(233) 445-66-77',
 *    '(233) 445-66-77',
 *    '(233) 445-66-77',
 *    '(233) 445-66-77',
 *    '(233) 445-66-77',
 *    '+1 (233) 445-66-77',
 *    '+1 (233) 445-66-77',
 *    '(233) 445-66-77',
 *    '+1 (233) 445-66-77',
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::format($phone, Phone::FORMAT_DEFAULT, false);
}
/**
 * [
 *    '6677',
 *    '4456677',
 *    '+1 (233) 445-66-77',
 *    '+ (233) 445-66-77',
 *    '+ (233) 445-66-77',
 *    '+ (233) 445-66-77',
 *    '+ (233) 445-66-77',
 *    '+ (233) 445-66-77',
 *    '+ (233) 445-66-77',
 *    '+1 (233) 445-66-77',
 *    '+1 (233) 445-66-77',
 *    '+ (233) 445-66-77',
 *    '+1 (233) 445-66-77',
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::format($phone, '$3-$4-$5');
}
/**
 * [
 *    '66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 *    '445-66-77',
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::format($phone, '$1-$2-$3-$4-$5', true, '(.{2})(.{2})(.{2})(.{4})(.*)');
}
/**
 * [
 *    '66-77',
 *    '4-45-66-77',
 *    '1-2334-45-66-77',
 *    '2334-45-66-77',
 *    '2334-45-66-77',
 *    '2334-45-66-77',
 *    '2334-45-66-77',
 *    '2334-45-66-77',
 *    '2334-45-66-77',
 *    '1-2334-45-66-77',
 *    '1-2334-45-66-77',
 *    '2334-45-66-77',
 *    '1-2334-45-66-77',
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::format($phone, '$1 $2 $3 $4-$5-$6', true,'(.{2})(.{2})(.{2})(.{2})(.{2})(.*)', [
        '$6' => '1$',
        '$5' => '2$',
        '$4' => '3$',
        '$3' => '4$',
        '$2' => '5$',
        '$1' => '6$'
    ]);
}
/**
 * [
 *    '66-77',
 *    '4 45-66-77',
 *    '1 23 34 45-66-77',
 *    '23 34 45-66-77',
 *    '23 34 45-66-77',
 *    '23 34 45-66-77',
 *    '23 34 45-66-77',
 *    '23 34 45-66-77',
 *    '23 34 45-66-77',
 *    '1 23 34 45-66-77',
 *    '1 23 34 45-66-77',
 *    '23 34 45-66-77',
 *    '1 23 34 45-66-77',
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::hide($phone);
}
/**
 * [
 *    '***-**-77',
 *    '***-**-77',
 *    '+1 (233) ***-**-77',
 *    '(233) ***-**-77',
 *    '(233) ***-**-77',
 *    '(233) ***-**-77',
 *    '(233) ***-**-77',
 *    '(233) ***-**-77',
 *    '(233) ***-**-77',
 *    '+1 (233) ***-**-77',
 *    '+1 (233) ***-**-77',
 *    '(233) ***-**-77',
 *    '+1 (233) ***-**-77',
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::hide($phone, '8-*** ***-**-$6');
}
/**
 * [
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 *    '8-*** ***-**-77',
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::is($phone);
}
/**
 * [
 *     false,
 *     true,
 *     false,
 *     true,
 *     false,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     false,
 *     false
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::is($phone, 11);
}
/**
 * [
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     true,
 *     true,
 *     false,
 *     false
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::is($phone, Phone::SIZE_MIN, 10);
}
/**
 * [
 *     false,
 *     true,
 *     false,
 *     false,
 *     false,
 *     true,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::exist($phone);
}
/**
 * [
 *     false,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::exist($phone, 11);
}
/**
 * [
 *     false,
 *     false,
 *     true,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     false,
 *     true,
 *     true,
 *     false,
 *     true
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::exist($phone, Phone::SIZE_MIN, 10);
}
/**
 * [
 *     false,
 *     true,
 *     false,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     true,
 *     false,
 *     false,
 *     true,
 *     false
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::extract($phone);
}
/**
 * [
 *     null,
 *     '4456677',
 *     '12334456677',
 *     '2334456677',
 *     '2334456677',
 *     '2334456677',
 *     '2334456677',
 *     '2334456677',
 *     '2334456677',
 *     '12334456677',
 *     '12334456677',
 *     '2334456677',
 *     '12334456677'
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::extract($phone, 11);
}
/**
 * [
 *     null,
 *     null,
 *     '12334456677',
 *     null,
 *     null,
 *     null,
 *     null,
 *     null,
 *     null,
 *     '12334456677',
 *     '12334456677',
 *     null,
 *     '12334456677'
 * ]
 */

$phones = [];
foreach($numbers as $phone)
{
    $phones[] = Phone::extract($phone, Phone::SIZE_MIN, 10);
}
/**
 * [
 *     null,
 *     '4456677',
 *     null,
 *     '2334456677',
 *     '2334456677',
 *     '2334456677',
 *     '2334456677',
 *     '2334456677',
 *     '2334456677',
 *     null,
 *     null,
 *     '2334456677',
 *     null
 * ]
 */
bash
phpunit PATH/TO/PROJECT/tests/
bash
phpunit --configuration PATH/TO/PROJECT/phpunit.xml