src/Phone_number.php
public function vn_convert_phone_number($phone_number = '', $phone_mode = '', $phone_format = NULL)
    {
        $inputParams = array(
            'phone_number' => $phone_number,
            'phone_mode'   => $phone_mode,
            'phone_format' => $phone_format
        );
        $this->debug->info(__FUNCTION__, 'Input Params: ', $inputParams);
        if (empty($phone_number)) {
            $this->debug->warning(__FUNCTION__, 'Phone Number input is Empty!');

            return NULL;
        }
        $mode = strtolower($phone_mode); // old || new
        // Convert Phone Number to CountryCode + NationalNumber
        $phone_number = $this->format(trim($phone_number));
        try {
            // Data Convert Phone Number
            $dataVnConvertPhoneNumber = Repository\DataRepository::getData('vn_convert_phone_number');
            $this->debug->debug(__FUNCTION__, 'Data Convert Phone Number: ', $dataVnConvertPhoneNumber);
            if (is_array($dataVnConvertPhoneNumber) && count($dataVnConvertPhoneNumber) > 0) {
                if ($mode == self::CONVERT_NEW_TO_OLD) {
                    $preg_match_number = self::MATCH_NUMBER_NEW;
                    $this->debug->debug(__FUNCTION__, 'Mode is New to Old with Preg Match: ', $preg_match_number);
                } elseif ($mode == self::CONVERT_OLD_TO_NEW) {
                    $preg_match_number = self::MATCH_NUMBER_OLD;
                    $this->debug->debug(__FUNCTION__, 'Mode is Old to New with Preg Match: ', $preg_match_number);
                } else {
                    $preg_match_number = NULL;
                }
                if ($preg_match_number !== NULL) {
                    if (!preg_match($preg_match_number, $phone_number)) {
                        $result = $this->format($phone_number, $phone_format);
                        $this->debug->warning(__FUNCTION__, 'Phone Number: ' . $phone_number . ' Invalid with Rule: ' . $preg_match_number . ' -> Output Result: ' . $result);

                        return $result;
                    }
                    foreach ($dataVnConvertPhoneNumber as $old_number_prefix => $new_number_prefix) {
                        if ($mode == self::CONVERT_NEW_TO_OLD) {
                            $phone_number_content = self::MAX_LENGTH_NUMBER_NEW - strlen($new_number_prefix); // 84 + number content
                            $phone_number_prefix  = $new_number_prefix;
                            $convert_prefix       = $old_number_prefix;
                            $this->debug->debug(__FUNCTION__, 'Data Convert New to Old: ', array(
                                'phone_number_content' => $phone_number_content,
                                'phone_number_prefix'  => $phone_number_prefix,
                                'convert_prefix'       => $convert_prefix
                            ));
                        } elseif ($mode == self::CONVERT_OLD_TO_NEW) {
                            $phone_number_content = self::MAX_LENGTH_NUMBER_OLD - strlen($old_number_prefix); // 84 + number content
                            $phone_number_prefix  = $old_number_prefix;
                            $convert_prefix       = $new_number_prefix;
                            $this->debug->debug(__FUNCTION__, 'Data Convert Old to New: ', array(
                                'phone_number_content' => $phone_number_content,
                                'phone_number_prefix'  => $phone_number_prefix,
                                'convert_prefix'       => $convert_prefix
                            ));
                        } else {
                            $phone_number_content = NULL;
                            $phone_number_prefix  = NULL;
                            $convert_prefix       = NULL;
                            $this->debug->debug(__FUNCTION__, 'Phone Number Invalid Rule Convert: ', array(
                                'phone_number_content' => $phone_number_content,
                                'phone_number_prefix'  => $phone_number_prefix,
                                'convert_prefix'       => $convert_prefix
                            ));
                        }
                        // Rule to check
                        $ruleCheckConvert = '/^(' . $phone_number_prefix . ')[0-9]{' . $phone_number_content . '}$/';
                        $this->debug->debug(__FUNCTION__, 'Rule preg_match Check to Convert Number: ' . $phone_number . ' is ', $ruleCheckConvert);
                        if (($phone_number_content !== NULL && $phone_number_prefix !== NULL && $convert_prefix !== NULL) && preg_match($ruleCheckConvert, $phone_number)) {
                            // Cắt lấy các số cuối tính từ vị trí đầu tiên trong dãy $phone_number rồi nối đầu số $convert_prefix
                            $phone_number = $convert_prefix . substr($phone_number, strlen($phone_number_prefix), $phone_number_content);
                            $this->debug->debug(__FUNCTION__, 'Rule Check OK -> Phone Number Convert: ', $phone_number);
                            $phone_number = $this->format($phone_number, $phone_format);
                            $this->debug->info(__FUNCTION__, 'Rule Check OK -> Phone Number Final Result Format: ', $phone_number);

                            return $phone_number;
                        }
                    }
                }
            } else {
                $message = 'Invalid or Unavailable Data Convert - Data: ';
                $this->debug->error(__FUNCTION__, $message, $dataVnConvertPhoneNumber);

                return $phone_number;
            }
        }
        catch (\Exception $e) {
            $message = 'Error File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Code: ' . $e->getCode() . ' - Message: ' . $e->getMessage();
            $this->debug->error(__FUNCTION__, $message);

            return NULL;
        }

        return NULL;
    }
public function vn_convert_phone_number($phone_number = '', $phone_mode = '', $phone_format = NULL)
    {
        $inputParams = array(
            'phone_number' => $phone_number,
            'phone_mode'   => $phone_mode,
            'phone_format' => $phone_format
        );
        $this->debug->info(__FUNCTION__, 'Input Params: ', $inputParams);
        if (empty($phone_number)) {
            $this->debug->warning(__FUNCTION__, 'Phone Number input is Empty!');

            return NULL;
        }
        $mode = strtolower($phone_mode); // old || new
        // Convert Phone Number to CountryCode + NationalNumber
        $phone_number = $this->format(trim($phone_number));
        try {
            // Data Convert Phone Number
            $dataVnConvertPhoneNumber = Repository\DataRepository::getData('vn_convert_phone_number');
            $this->debug->debug(__FUNCTION__, 'Data Convert Phone Number: ', $dataVnConvertPhoneNumber);
            if (is_array($dataVnConvertPhoneNumber) && count($dataVnConvertPhoneNumber) > 0) {
                if ($mode == self::CONVERT_NEW_TO_OLD) {
                    $preg_match_number = self::MATCH_NUMBER_NEW;
                    $this->debug->debug(__FUNCTION__, 'Mode is New to Old with Preg Match: ', $preg_match_number);
                } elseif ($mode == self::CONVERT_OLD_TO_NEW) {
                    $preg_match_number = self::MATCH_NUMBER_OLD;
                    $this->debug->debug(__FUNCTION__, 'Mode is Old to New with Preg Match: ', $preg_match_number);
                } else {
                    $preg_match_number = NULL;
                }
                if ($preg_match_number !== NULL) {
                    if (!preg_match($preg_match_number, $phone_number)) {
                        $result = $this->format($phone_number, $phone_format);
                        $this->debug->warning(__FUNCTION__, 'Phone Number: ' . $phone_number . ' Invalid with Rule: ' . $preg_match_number . ' -> Output Result: ' . $result);

                        return $result;
                    }
                    foreach ($dataVnConvertPhoneNumber as $old_number_prefix => $new_number_prefix) {
                        if ($mode == self::CONVERT_NEW_TO_OLD) {
                            $phone_number_content = self::MAX_LENGTH_NUMBER_NEW - strlen($new_number_prefix); // 84 + number content
                            $phone_number_prefix  = $new_number_prefix;
                            $convert_prefix       = $old_number_prefix;
                            $this->debug->debug(__FUNCTION__, 'Data Convert New to Old: ', array(
                                'phone_number_content' => $phone_number_content,
                                'phone_number_prefix'  => $phone_number_prefix,
                                'convert_prefix'       => $convert_prefix
                            ));
                        } elseif ($mode == self::CONVERT_OLD_TO_NEW) {
                            $phone_number_content = self::MAX_LENGTH_NUMBER_OLD - strlen($old_number_prefix); // 84 + number content
                            $phone_number_prefix  = $old_number_prefix;
                            $convert_prefix       = $new_number_prefix;
                            $this->debug->debug(__FUNCTION__, 'Data Convert Old to New: ', array(
                                'phone_number_content' => $phone_number_content,
                                'phone_number_prefix'  => $phone_number_prefix,
                                'convert_prefix'       => $convert_prefix
                            ));
                        } else {
                            $phone_number_content = NULL;
                            $phone_number_prefix  = NULL;
                            $convert_prefix       = NULL;
                            $this->debug->debug(__FUNCTION__, 'Phone Number Invalid Rule Convert: ', array(
                                'phone_number_content' => $phone_number_content,
                                'phone_number_prefix'  => $phone_number_prefix,
                                'convert_prefix'       => $convert_prefix
                            ));
                        }
                        // Rule to check
                        $ruleCheckConvert = '/^(' . $phone_number_prefix . ')[0-9]{' . $phone_number_content . '}$/';
                        $this->debug->debug(__FUNCTION__, 'Rule preg_match Check to Convert Number: ' . $phone_number . ' is ', $ruleCheckConvert);
                        if (($phone_number_content !== NULL && $phone_number_prefix !== NULL && $convert_prefix !== NULL) && preg_match($ruleCheckConvert, $phone_number)) {
                            // Cắt lấy các số cuối tính từ vị trí đầu tiên trong dãy $phone_number rồi nối đầu số $convert_prefix
                            $phone_number = $convert_prefix . substr($phone_number, strlen($phone_number_prefix), $phone_number_content);
                            $this->debug->debug(__FUNCTION__, 'Rule Check OK -> Phone Number Convert: ', $phone_number);
                            $phone_number = $this->format($phone_number, $phone_format);
                            $this->debug->info(__FUNCTION__, 'Rule Check OK -> Phone Number Final Result Format: ', $phone_number);

                            return $phone_number;
                        }
                    }
                }
            } else {
                $message = 'Invalid or Unavailable Data Convert - Data: ';
                $this->debug->error(__FUNCTION__, $message, $dataVnConvertPhoneNumber);

                return $phone_number;
            }
        }
        catch (\Exception $e) {
            $message = 'Error File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Code: ' . $e->getCode() . ' - Message: ' . $e->getMessage();
            $this->debug->error(__FUNCTION__, $message);

            return NULL;
        }

        return NULL;
    }
public function vn_convert_phone_number ( $ phone_number = '' , $ phone_mode = '' , $ phone_format = NULL ) { $ inputParams = array ( 'phone_number' => $ phone_number , 'phone_mode' => $ phone_mode , 'phone_format' => $ phone_format ) ; $ this -> debug -> info ( __FUNCTION__ , 'Input Params: ' , $ inputParams ) ; if ( empty ( $ phone_number ) ) { $ this -> debug -> warning ( __FUNCTION__ , 'Phone Number input is Empty!' ) ; return NULL ; } $ mode = strtolower ( $ phone_mode ) ; // old || new // Convert Phone Number to CountryCode + NationalNumber $ phone_number = $ this -> format ( trim ( $ phone_number ) ) ; try { // Data Convert Phone Number $ dataVnConvertPhoneNumber = Repository \ DataRepository :: getData ( 'vn_convert_phone_number' ) ; $ this -> debug -> debug ( __FUNCTION__ , 'Data Convert Phone Number: ' , $ dataVnConvertPhoneNumber ) ; if ( is_array ( $ dataVnConvertPhoneNumber ) && count ( $ dataVnConvertPhoneNumber ) > 0 ) { if ( $ mode == self :: CONVERT_NEW_TO_OLD ) { $ preg_match_number = self :: MATCH_NUMBER_NEW ; $ this -> debug -> debug ( __FUNCTION__ , 'Mode is New to Old with Preg Match: ' , $ preg_match_number ) ; } elseif ( $ mode == self :: CONVERT_OLD_TO_NEW ) { $ preg_match_number = self :: MATCH_NUMBER_OLD ; $ this -> debug -> debug ( __FUNCTION__ , 'Mode is Old to New with Preg Match: ' , $ preg_match_number ) ; } else { $ preg_match_number = NULL ; } if ( $ preg_match_number !== NULL ) { if ( ! preg_match ( $ preg_match_number , $ phone_number ) ) { $ result = $ this -> format ( $ phone_number , $ phone_format ) ; $ this -> debug -> warning ( __FUNCTION__ , 'Phone Number: ' . $ phone_number . ' Invalid with Rule: ' . $ preg_match_number . ' -> Output Result: ' . $ result ) ; return $ result ; } foreach ( $ dataVnConvertPhoneNumber as $ old_number_prefix => $ new_number_prefix ) { if ( $ mode == self :: CONVERT_NEW_TO_OLD ) { $ phone_number_content = self :: MAX_LENGTH_NUMBER_NEW - strlen ( $ new_number_prefix ) ; // 84 + number content $ phone_number_prefix = $ new_number_prefix ; $ convert_prefix = $ old_number_prefix ; $ this -> debug -> debug ( __FUNCTION__ , 'Data Convert New to Old: ' , array ( 'phone_number_content' => $ phone_number_content , 'phone_number_prefix' => $ phone_number_prefix , 'convert_prefix' => $ convert_prefix ) ) ; } elseif ( $ mode == self :: CONVERT_OLD_TO_NEW ) { $ phone_number_content = self :: MAX_LENGTH_NUMBER_OLD - strlen ( $ old_number_prefix ) ; // 84 + number content $ phone_number_prefix = $ old_number_prefix ; $ convert_prefix = $ new_number_prefix ; $ this -> debug -> debug ( __FUNCTION__ , 'Data Convert Old to New: ' , array ( 'phone_number_content' => $ phone_number_content , 'phone_number_prefix' => $ phone_number_prefix , 'convert_prefix' => $ convert_prefix ) ) ; } else { $ phone_number_content = NULL ; $ phone_number_prefix = NULL ; $ convert_prefix = NULL ; $ this -> debug -> debug ( __FUNCTION__ , 'Phone Number Invalid Rule Convert: ' , array ( 'phone_number_content' => $ phone_number_content , 'phone_number_prefix' => $ phone_number_prefix , 'convert_prefix' => $ convert_prefix ) ) ; } // Rule to check $ ruleCheckConvert = '/^(' . $ phone_number_prefix . ')[0-9]{' . $ phone_number_content . '}$/' ; $ this -> debug -> debug ( __FUNCTION__ , 'Rule preg_match Check to Convert Number: ' . $ phone_number . ' is ' , $ ruleCheckConvert ) ; if ( ( $ phone_number_content !== NULL && $ phone_number_prefix !== NULL && $ convert_prefix !== NULL ) && preg_match ( $ ruleCheckConvert , $ phone_number ) ) { // Cắt lấy các số cuối tính từ vị trí đầu tiên trong dãy $phone_number rồi nối đầu số $convert_prefix $ phone_number = $ convert_prefix . substr ( $ phone_number , strlen ( $ phone_number_prefix ) , $ phone_number_content ) ; $ this -> debug -> debug ( __FUNCTION__ , 'Rule Check OK -> Phone Number Convert: ' , $ phone_number ) ; $ phone_number = $ this -> format ( $ phone_number , $ phone_format ) ; $ this -> debug -> info ( __FUNCTION__ , 'Rule Check OK -> Phone Number Final Result Format: ' , $ phone_number ) ; return $ phone_number ; } } } } else { $ message = 'Invalid or Unavailable Data Convert - Data: ' ; $ this -> debug -> error ( __FUNCTION__ , $ message , $ dataVnConvertPhoneNumber ) ; return $ phone_number ; } } catch ( \ Exception $ e ) { $ message = 'Error File: ' . $ e -> getFile ( ) . ' - Line: ' . $ e -> getLine ( ) . ' - Code: ' . $ e -> getCode ( ) . ' - Message: ' . $ e -> getMessage ( ) ; $ this -> debug -> error ( __FUNCTION__ , $ message ) ; return NULL ; } return NULL ; }
Function vn_convert_phone_number
Convert Phone Number old to new or new to old
Chuyển đổi Số điện thoại từ định dạng mới sang cũ hoặc ngược lại

@author: 713uk13m <dev@nguyenanhung.com>
@time  : 9/21/18 01:32

@param string $phone_number This is Phone number
@param string $phone_mode   This mode as old or new
@param null   $phone_format This format vn or other, list keyword: VN, VN_HUMAN, E164, INTERNATIONAL, NATIONAL,
RFC3966, HIDDEN, HIDDEN_HEAD, HIDDEN_MIDDLE, HIDDEN_END

@see   https://github.com/nguyenanhung/vn-telco-phonenumber/blob/master/test_phone_number.php

@return null|string
Function vn_convert_phone_number Convert Phone Number old to new or new to old Chuyển đổi Số điện thoại từ định dạng mới sang cũ hoặc ngược lại