1、判断是否正确的日期格式
/**
* 是否正确的日期
*
* @access public
*/
private function _isdate($str,$format="Y-m-d H:i")
{
$strArr = explode("-",$str);
if(empty($strArr))
{
return false;
}
foreach($strArr as $val)
{
if(strlen($val)<2)
{
$val="0".$val;
}
$newArr[]=$val;
}
$str =implode("-",$newArr);
$unixTime=strtotime($str);
$checkDate= date($format,$unixTime);
if($checkDate==$str)
{
return true;
}
else
{
return false;
}
}
2、汉字转整型数字,如:三百八十六 转成 386(from:http://blog.csdn.net/sui13725202/article/details/11581989)
test(); /**
* 测试
*/
function test() {
echo CnToInt('一'); //
echo CnToInt('十'); //
echo CnToInt('十一'); //
echo CnToInt('一百一十'); //
echo CnToInt('一千零一'); //
echo CnToInt('一万零一百零一'); //
echo CnToInt('一亿一千三百万零三千零一'); //
echo CnToInt('一千万亿'); // 11.0E+15
} /**
* 中文转数字
* @param String $var 需要解析的中文数
* @param Int $start 初始值
* @return int
*/
function CnToInt($var, $start = 0) {
if (is_numeric($var)) {
return $var;
}
if (intval($var) === 0) {
$splits = array('亿' => 100000000, '万' => 10000);
$chars = array('万' => 10000, '千' => 1000, '百' => 100, '十' => 10, '一' => 1, '零' => 0);
$Ints = array('零' => 0, '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5, '六' => 6, '七' => 7, '八' => 8, '九' => 9, '十' => 10);
$var = str_replace('零', "", $var);
foreach ($splits as $key => $step) {
if (strpos($var, $key)) {
$strs = explode($key, $var);
$start += CnToInt(array_shift($strs)) * $step;
$var = join('', $strs);
}
}
foreach ($chars as $key => $step) {
if (strpos($var, $key) !== FALSE) {
$vs = explode($key, $var);
if ($vs[0] === "") {
$vs[0] = '一';
}
$start += $Ints[array_shift($vs)] * $step;
$var = join('', $vs);
} elseif (mb_strlen($var, 'utf-8') === 1) {
$start += $Ints[$var];
$var = '';
break;
}
}
return $start;
} else {
return intval($var);
}
}
3、生成唯一的值:uniqid
4、中文截取:
function self_substr($str, $len = 12, $dot = true)
{
$i = 0;
$l = 0;
$c = 0;
$a = array();
while ($l < $len)
{
$t = substr($str, $i, 1);
if (ord($t) >= 224)
{
$c = 3;
$t = substr($str, $i, $c);
$l += 2;
}
elseif (ord($t) >= 192)
{
$c = 2;
$t = substr($str, $i, $c);
$l += 2;
}
else
{
$c = 1;
$l++;
}
// $t = substr($str, $i, $c);
$i += $c;
if ($l > $len) break;
$a[] = $t;
}
$re = implode('', $a);
if (substr($str, $i, 1) !== false)
{
array_pop($a);
($c == 1) and array_pop($a);
$re = implode('', $a);
$dot and $re .= '...';
}
return $re;
}
5、二维数组的排序
function array_sort($arr,$keys,$type='asc')
{
$keysvalue = $new_array = array();
foreach ($arr as $k=>$v) {
$keysvalue[$k] = $v[$keys];
}
if($type == 'asc') {
asort($keysvalue);
} else {
arsort($keysvalue);
}
reset($keysvalue);
foreach($keysvalue as $k=>$v) {
$new_array[$k] = $arr[$k];
} $arr = array();
foreach($new_array as $key => $val) {
$arr[] = $val;
}
return $arr;
}