PHP code example of kariha / tr-phone

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

    

kariha / tr-phone example snippets




use Kariha\TrPhone\PhoneNumber;

$phone = PhoneNumber::parse('0555 000 00 00');

echo $phone->e164();         // +905550000000
echo $phone->national();     // 0555 000 00 00
echo $phone->international(); // +90 555 000 00 00
echo $phone->digits();       // 5550000000

use Kariha\TrPhone\PhoneNumber;

if (PhoneNumber::tryParse($input) !== null) {
    // The input is a recognized Turkish phone number.
}

use Kariha\TrPhone\TrPhone;

if (TrPhone::validate('+90 532 123 45 67')) {
    // Valid Turkish phone number.
}

use Kariha\TrPhone\TrPhone;

echo TrPhone::normalize('(0555) 000 00 00'); // 5550000000

use Kariha\TrPhone\Format;
use Kariha\TrPhone\PhoneNumber;

$phone = PhoneNumber::parse('0555 000 00 00');

echo $phone->format(Format::E164);          // +905550000000
echo $phone->format(Format::NATIONAL);      // 0555 000 00 00
echo $phone->format(Format::INTERNATIONAL); // +90 555 000 00 00
echo $phone->format(Format::COMPACT);       // 5550000000

$phone = PhoneNumber::parse('0532 123 45 67');

$phone->isMobile(); // true

$phone = PhoneNumber::parse('0212 123 45 67');

$phone->isLandline(); // true
$phone->areaName();   // Istanbul Europe

$phone = PhoneNumber::parse('0532 123 45 67');

echo $phone->prefix();         // 532
echo $phone->prefixOperator(); // Turkcell
echo $phone->prefixService();  // Mobile electronic communication service

use Kariha\TrPhone\Exception\InvalidPhoneNumberException;
use Kariha\TrPhone\PhoneNumber;

try {
    $phone = PhoneNumber::parse($input);
} catch (InvalidPhoneNumberException $exception) {
    // Show a user-friendly validation message or log the reason.
}

$phone = PhoneNumber::tryParse($input);

if ($phone === null) {
    // Invalid or unsupported number.
}