當前位置: 首頁>>代碼示例>>PHP>>正文


PHP PhoneNumberUtil::formatByPattern方法代碼示例

本文整理匯總了PHP中libphonenumber\PhoneNumberUtil::formatByPattern方法的典型用法代碼示例。如果您正苦於以下問題:PHP PhoneNumberUtil::formatByPattern方法的具體用法?PHP PhoneNumberUtil::formatByPattern怎麽用?PHP PhoneNumberUtil::formatByPattern使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在libphonenumber\PhoneNumberUtil的用法示例。


在下文中一共展示了PhoneNumberUtil::formatByPattern方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testFormatByPattern

 public function testFormatByPattern()
 {
     $newNumFormat = new NumberFormat();
     $newNumFormat->setPattern("(\\d{3})(\\d{3})(\\d{4})");
     $newNumFormat->setFormat("(\$1) \$2-\$3");
     $newNumberFormats = array();
     $newNumberFormats[] = $newNumFormat;
     $this->assertEquals("(650) 253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $this->assertEquals("+1 (650) 253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats));
     $this->assertEquals("tel:+1-650-253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::RFC3966, $newNumberFormats));
     // $NP is set to '1' for the US. Here we check that for other NANPA countries the US rules are
     // followed.
     $newNumFormat->setNationalPrefixFormattingRule('$NP ($FG)');
     $newNumFormat->setFormat("\$1 \$2-\$3");
     $this->assertEquals("1 (242) 365-1234", $this->phoneUtil->formatByPattern(self::$bsNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $this->assertEquals("+1 242 365-1234", $this->phoneUtil->formatByPattern(self::$bsNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats));
     $newNumFormat->setPattern("(\\d{2})(\\d{5})(\\d{3})");
     $newNumFormat->setFormat("\$1-\$2 \$3");
     $newNumberFormats[0] = $newNumFormat;
     $this->assertEquals("02-36618 300", $this->phoneUtil->formatByPattern(self::$itNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $this->assertEquals("+39 02-36618 300", $this->phoneUtil->formatByPattern(self::$itNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats));
     $newNumFormat->setNationalPrefixFormattingRule('$NP$FG');
     $newNumFormat->setPattern("(\\d{2})(\\d{4})(\\d{4})");
     $newNumFormat->setFormat("\$1 \$2 \$3");
     $newNumberFormats[0] = $newNumFormat;
     $this->assertEquals("020 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $newNumFormat->setNationalPrefixFormattingRule('($NP$FG)');
     $this->assertEquals("(020) 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $newNumFormat->setNationalPrefixFormattingRule("");
     $this->assertEquals("20 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $this->assertEquals("+44 20 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats));
 }
開發者ID:wasabiNorman,項目名稱:libphonenumber-for-php,代碼行數:32,代碼來源:PhoneNumberUtilTest.php


注:本文中的libphonenumber\PhoneNumberUtil::formatByPattern方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。