本文整理匯總了PHP中Idna::encode方法的典型用法代碼示例。如果您正苦於以下問題:PHP Idna::encode方法的具體用法?PHP Idna::encode怎麽用?PHP Idna::encode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Idna
的用法示例。
在下文中一共展示了Idna::encode方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: idnaEncode
/**
* Encode an internationalized domain name
*
* @param string $strDomain The domain name
*
* @return string The encoded domain name
*
* @deprecated Use Idna::encode() instead
*/
protected function idnaEncode($strDomain)
{
return \Idna::encode($strDomain);
}
示例2: parse
/**
* Tries to parse a string and to get the domain name, tld and idn
* converted domain name.
*
* If given string is not a domain name, it will add a default tld.
*
* Also skips given string if it is longer than 63 characters.
*
* @throws instance of AbstractException if throwExceptions = true
* @param string $unparsedString
* @param string $defaultTld
* @return void
*/
public function parse($unparsedString, $defaultTld = 'com')
{
try {
if ($this->loaded === false) {
$this->load();
}
$matchedDomain = '';
$matchedDomainIdn = '';
$matchedTld = '';
$matchedTldIdn = '';
$validHostname = true;
$IdnaConverter = new Idna(array('idn_version' => 2008));
preg_match('/^((http|https|ftp|ftps|news|ssh|sftp|gopher):[\\/]{2,})?([^\\/]+)/', mb_strtolower(trim($unparsedString), $this->encoding), $matches);
$parsedString = end($matches);
foreach ($this->tldList['content'] as $tld) {
if (preg_match('/\\.' . $tld . '$/', $parsedString, $trash)) {
$matchedTld = $tld;
$matchedTldIdn = $IdnaConverter->encode($tld);
$matchedDomain = str_replace('.' . $matchedTld, '', $parsedString);
$matchedDomain = rtrim($matchedDomain, '.');
$matchedDomain = ltrim($matchedDomain, '.');
if ($matchedTld != 'name' && strpos($matchedDomain, '.')) {
$matchedDomain = str_replace('.', '', strrchr($matchedDomain, '.'));
}
if (strpos($matchedDomain, ' ')) {
$matchedDomain = explode(' ', $matchedDomain);
$matchedDomain = end($matchedDomain);
}
$matchedDomainIdn = $IdnaConverter->encode($matchedDomain);
break;
}
if ($tld == $parsedString) {
$matchedTld = $tld;
$matchedTldIdn = $IdnaConverter->encode($tld);
break;
}
}
if ($matchedDomain == '' && strlen($matchedDomainIdn) <= 63 && $matchedTld == '') {
$matchedDomain = $IdnaConverter->decode(preg_replace_callback('/[^a-zA-Z0-9\\-\\.]/', function ($match) use(&$validHostname) {
$validHostname = false;
}, $IdnaConverter->encode($parsedString)));
$matchedDomainIdn = $IdnaConverter->encode($matchedDomain);
$matchedTld = $matchedTldIdn = $defaultTld;
} elseif ($matchedDomain != '' && strlen($matchedDomainIdn) <= 63 && $matchedTld != '') {
$matchedDomain = $IdnaConverter->decode(preg_replace_callback('/[^a-zA-Z0-9\\-\\.]/', function ($match) use(&$validHostname) {
$validHostname = false;
}, $IdnaConverter->encode($matchedDomain)));
$matchedDomainIdn = $IdnaConverter->encode($matchedDomain);
} elseif ($matchedDomain == '' && $matchedTld != '') {
$validHostname = false;
} else {
throw \DomainParser\AbstractException::factory('UnparsableString', 'Unparsable domain name.');
}
$Result = new Result($matchedDomain, $matchedDomainIdn, $matchedTld, $matchedTldIdn, $validHostname);
} catch (\DomainParser\AbstractException $e) {
if ($this->throwExceptions) {
throw $e;
}
$Result = new Result();
$Result->error = $e->getMessage();
}
return $Result->get($this->format);
}
示例3: catchTlds
/**
* Catch list from server and parse them to array.
*
* It only uses the official ICANN domain names and adds private
* domains and missing official third-levels by using an additional hash.
*
* The manual added list is not complete.
*
* @throws ConnectErrorException
* @see Novutec\Additional.php $additional
* @param boolean $existFile
* @return void
*/
private function catchTlds($existFile)
{
$content = @file_get_contents($this->tldUrl);
if ($content === false) {
if (!$existFile) {
throw \Novutec\DomainParser\AbstractException::factory('Connect', 'Could not catch file from server.');
}
return;
}
$IdnaConverter = new Idna(array('idn_version' => 2008));
// only match official ICANN domain tlds
if (preg_match('/\\/\\/ ===BEGIN ICANN DOMAINS===(.*)(?=\\/\\/ ===END ICANN DOMAINS===)/s', $content, $matches) !== 1) {
throw \Novutec\DomainParser\AbstractException::factory('UnparsableString', 'Could not fetch ICANN Domains of Mozilla TLD File.');
}
$tlds = array();
$list_str = $matches[1];
foreach (explode("\n", $list_str) as $line) {
$line = trim($line);
// skip empty or comment lines
if ($line == '' || $line[0] == '/' || strpos($line, '!') !== false) {
continue;
}
// reformat prefixed wildcards
if ($line[0] == '*') {
$line = substr($line, 2);
}
// convert to xn-- format
$tld = $IdnaConverter->encode($line);
// validate if toplevel domain
$pos = strrpos($tld, '.');
if ($pos === false) {
$match = $tld;
} else {
$match = substr($tld, $pos + 1);
}
if (!isset($tlds[$match])) {
$tlds[$match] = array();
}
$tlds[$match][] = $tld;
}
// load additional to add to list
require_once 'Additional.php';
// merge list and sort tlds by length within its group
$this->tldList['content'] = array_merge_recursive($tlds, $additional);
foreach ($this->tldList['content'] as $tldGroup => $tld) {
usort($tld, function ($a, $b) {
return strlen($b) - strlen($a);
});
$this->tldList['content'][$tldGroup] = $tld;
}
$this->tldList['timestamp'] = time();
}
示例4: test_encode
public function test_encode()
{
foreach ($this->arrIdnaTests as $strDefaultDomain => $strEncodedDomain) {
$this->assertEquals(Idna::encode($strDefaultDomain), $strEncodedDomain);
}
}