本文整理匯總了PHP中utf8::str_split方法的典型用法代碼示例。如果您正苦於以下問題:PHP utf8::str_split方法的具體用法?PHP utf8::str_split怎麽用?PHP utf8::str_split使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類utf8
的用法示例。
在下文中一共展示了utf8::str_split方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: random
/**
* Generates a random string of a given type and length.
*
* @param string a type of pool, or a string of characters to use as the pool
* @param integer length of string to return
* @return string
*
* @tutorial alnum alpha-numeric characters
* @tutorial alpha alphabetical characters
* @tutorial hexdec hexadecimal characters, 0-9 plus a-f
* @tutorial numeric digit characters, 0-9
* @tutorial nozero digit characters, 1-9
* @tutorial distinct clearly distinct alpha-numeric characters
*/
public static function random($type = 'alnum', $length = 8)
{
$utf8 = FALSE;
switch ($type) {
case 'alnum':
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'hexdec':
$pool = '0123456789abcdef';
break;
case 'numeric':
$pool = '0123456789';
break;
case 'nozero':
$pool = '123456789';
break;
case 'distinct':
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
break;
default:
$pool = (string) $type;
$utf8 = !utf8::is_ascii($pool);
break;
}
// Split the pool into an array of characters
$pool = $utf8 === TRUE ? utf8::str_split($pool, 1) : str_split($pool, 1);
// Largest pool key
$max = count($pool) - 1;
$str = '';
for ($i = 0; $i < $length; $i++) {
// Select a random character from the pool and add it to the string
$str .= $pool[mt_rand(0, $max)];
}
// Make sure alnum strings contain at least one letter and one digit
if ($type === 'alnum' and $length > 1) {
if (ctype_alpha($str)) {
// Add a random digit
$str[mt_rand(0, $length - 1)] = chr(mt_rand(48, 57));
} elseif (ctype_digit($str)) {
// Add a random letter
$str[mt_rand(0, $length - 1)] = chr(mt_rand(65, 90));
}
}
return $str;
}
示例2: str_split
/**
* Tests the utf8::str_split() function.
* @dataProvider str_split_provider
* @group core.helpers.utf8.str_split
* @test
*/
public function str_split($str, $split_length, $expected_result)
{
$result = utf8::str_split($str, $split_length);
$this->assertEquals($expected_result, $result);
}