Last active
June 4, 2016 09:18
-
-
Save azhai/3fbb74188e453d1b3011 to your computer and use it in GitHub Desktop.
words.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 开始的字符串相同 | |
* | |
* @param string $haystack 可能包含子串的字符串 | |
* @param string $needle 要查找的子串 | |
* @return bool | |
*/ | |
function starts_with($haystack, $needle) | |
{ | |
return strncmp($haystack, $needle, strlen($needle)) === 0; | |
} | |
/** | |
* 结束的字符串相同 | |
* | |
* @param string $haystack 可能包含子串的字符串 | |
* @param string $needle 要查找的子串 | |
* @return bool | |
*/ | |
function ends_with($haystack, $needle) | |
{ | |
$ndlen = strlen($needle); | |
return $ndlen === 0 || (strlen($haystack) >= $ndlen && | |
substr_compare($haystack, $needle, -$ndlen) === 0); | |
} | |
/** | |
* 前N天的日期 | |
* | |
* @param int $ago 往前数几天 | |
* @param string $refer 参照日期 | |
* @return string | |
*/ | |
function day_ago($ago = 1, $refer = null) | |
{ | |
$time = $refer ? strtotime($refer) : time(); | |
return date('Y-m-d', $time - 86400 * intval($ago)); | |
} | |
/** | |
* 求百分比 | |
* | |
* @param float $dividend 被除数 | |
* @param float $divider 除数,不能为0 | |
* @return string | |
*/ | |
function to_percent($dividend, $divider = 1) | |
{ | |
$divider = floatval($divider); | |
if ($divider === 0.0) { | |
return ''; | |
} | |
$dividend = floatval($dividend); | |
if ($dividend === 0.0) { | |
$percent = 0.0; | |
} else { | |
$percent = $dividend / $divider * 100; | |
} | |
return sprintf('%.1f', $percent) . '%'; | |
} | |
/** | |
* 获取首字母区域 | |
* 0 数字或ASCII符号 | |
* 1 a-d 2 e-g 3 h-k 4 l-n 5 o-q 6 r-t 7 u-w 8 x-z | |
* 9 不常见汉字或无法识别 | |
* | |
* @param string $cnchar 单个汉字 | |
* @return int | |
*/ | |
function get_letter_area($cnchar) | |
{ | |
static $letter_areas = array( | |
1 => '数字/字母', 674 => '拼音-ABCD', 994 => '拼音-EFG', | |
1612 => '拼音-HIJK', 2122 => '拼音-LMN', 2427 => '拼音-OPQ', | |
2958 => '拼音-RST', 3084 => '拼音-UVW', 3989 => '拼音-XYZ' | |
); | |
$gbk = mb_convert_encoding($cnchar, 'GBK'); | |
if (strlen($gbk) < 2) { //ASCII字符 | |
return $letter_areas[1]; | |
} | |
$high_code = ord(substr($gbk, 0, 1)) - 160; //GBK区码 | |
$low_code = ord(substr($gbk, 1, 1)) - 160; //GBK位码 | |
$char_code = ($high_code - 16) * 100 + $low_code; | |
$i = 0; | |
foreach ($letter_areas as $start => $label) { | |
if ($char_code < $start) { | |
return $label; | |
} | |
$i ++; | |
} | |
return '不常见汉字'; | |
} | |
/** | |
* 对下拉列表按显示文本排序 | |
* | |
* @param array $opts 列表项(不要包含“全部”) | |
*/ | |
function options_sort(array $opts) | |
{ | |
uasort($opts, create_function('$a,$b', | |
'return strcmp(mb_convert_encoding(strtolower($a),"GBK"),' | |
.'mb_convert_encoding(strtolower($b),"GBK"));' | |
)); | |
return $opts; | |
} | |
/** | |
* 对下拉列表按显示文本排序并分组 | |
* | |
* @param array $opts 列表项(不要包含“全部”) | |
*/ | |
function options_group(array $opts) | |
{ | |
$opts = options_sort($opts); | |
$grouped = array(); | |
foreach ($opts as $value => $text) { | |
$label = get_letter_area(mb_substr($text, 0, 1)); | |
if (! isset($grouped[$label])) { | |
$grouped[$label] = array(); | |
} | |
$grouped[$label][$value] = $text; | |
} | |
return $grouped; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment