一些常用实用的PHP方法汇总集合(一)
-
检查字符串是否包含在数组里面
/**
* @param string $findString 查询的字符串
* @param array $findAry 匹配的数组
* @return string
*/
function strpos_array($findString, $findAry) {
if (is_array($findAry)) {
foreach ($findAry as $str) {
if (is_array($str)) {
$pos = strpos_array($findString, $str);
} else {
$pos = strpos($findString, $str);
}
if ($pos !== FALSE) {
return $pos;
}
}
} else {
return strpos($findString, $findAry);
}
}
-
获取随机字符串
/**
* @param int $len 需要的长度
* @return string
*/
function getRandStr($len) {
$chars_array = array(
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z",
);
$charsLen = count($chars_array) - 1;
$outPutStr = "";
for ($i = 0; $i < $len; $i++) {
$outPutStr .= $chars_array[mt_rand(0, $charsLen)];
}
return $outputstr;
}
-
计算两个日期的时间差
/**
* @param date $begin_time 开始时间
* @param date $end_time 结束时间
* @return array
*/
function DiffDateTime($start, $end) {
$begin_time = strtotime($start);
$end_time = strtotime($end);
if ($begin_time < $end_time) {
$starttime = $begin_time;
$endtime = $end_time;
} else {
$starttime = $end_time;
$endtime = $begin_time;
}
$timediff = $endtime - $starttime;
$days = intval($timediff / 86400);
$remain = $timediff % 86400;
$hours = intval($remain / 3600);
$remain = $remain % 3600;
$mins = intval($remain / 60);
$secs = $remain % 60;
$res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
return $res;
}
-
PHP数组转树形结构数据
/**
* @param array $arr 数组
* @param int $fid 主键ID
* @param int $fparent 父类ID
* @return array
*/
function tree_array($arr, $fid, $fparent = 'pid', $fchildrens = 'child', $returnReferences = false) {
$pkvRefs = array();
foreach ($arr as $offset => $row) {
$pkvRefs[$row[$fid]] = &$arr[$offset];
$pkvRefs[$row[$fid]][$fchildrens] = array();
}
$tree = array();
foreach ($arr as $offset => $row) {
$parentId = $row[$fparent];
if ($parentId) {
if (!isset($pkvRefs[$parentId])) {
continue;
}
$parent = &$pkvRefs[$parentId];
$parent[$fchildrens][] = &$arr[$offset];
} else {
$tree[] = &$arr[$offset];
}
}
if ($returnReferences) {
return array('tree' => $tree, 'refs' => $pkvRefs);
} else {
return $tree;
}
} -
字符串(手机号码)替换星号(*)
/**
* @param string $str 要替换的字符串
* @param int $start 开始位置
* @param int $length 替换长度
* @return string
*/
function replaceStar($str, $start, $length = 0) {
$i = 0;
$star = '';
if ($start >= 0) {
if ($length > 0) {
$str_len = strlen($str);
$count = $length;
if ($start >= $str_len) {
$count = 0;
}
} elseif ($length < 0) {
$str_len = strlen($str);
$count = abs($length);
if ($start >= $str_len) {
$start = $str_len - 1;
}
$offset = $start - $count + 1;
$count = $offset >= 0 ? abs($length) : ($start + 1);
$start = $offset >= 0 ? $offset : 0;
} else {
$str_len = strlen($str);
$count = $str_len - $start;
}
} else {
if ($length > 0) {
$offset = abs($start);
$count = $offset >= $length ? $length : $offset;
} elseif ($length < 0) {
$str_len = strlen($str);
$end = $str_len + $start;
$offset = abs($start + $length) - 1;
$start = $str_len - $offset;
$start = $start >= 0 ? $start : 0;
$count = $end - $start + 1;
} else {
$str_len = strlen($str);
$count = $str_len + $start + 1;
$start = 0;
}
}
while ($i < $count) {
$star .= '*';
$i++;
}
return substr_replace($str, $star, $start, $count);
}