PHP常用函数收集

1、出生日期转换成年龄

private function changeBirthdayToAge($birthday)
{
$interval = date(time() - strtotime($birthday));
return intval($interval / (365 * 60 * 60 * 24 )) + 1;
}


2、object转换成array
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}

if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}


3、array转换成object
function arrayToObject($d) {
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return (object) array_map(__FUNCTION__, $d);
}
else {
// Return object
return $d;
}
}


4、日期格式化
function microtime_format() {
$time = number_format(microtime(true),8,'.','');
return explode(".", $time);
}


5、HTML格式编码转换
function htmlspecialcharsx($str) {
$s = htmlspecialchars($str);
return str_replace('&#', '&#', $s);
}


6、当前日期
function datetime() {
return date("Y-m-d H:i:s");
}


7、多维数组排序
function multi_array_sort($multi_array, $sort_key, $sort=SORT_ASC){ 
if(is_array($multi_array)){
foreach ($multi_array as $row_array){
if(is_array($row_array)){
$key_array[] = $row_array[$sort_key];
} else {
return false;
}
}
} else {
return false;
}
array_multisort($key_array, $sort, $multi_array);
return $multi_array;
}


8、创建目录

/**
+----------------------------------------------------------
* 创建目录
+----------------------------------------------------------
* @access private
* @param dir 目录地址
* @return boolean
+----------------------------------------------------------
*/
public function createDir($dir)
{
// 創建目錄
if (!is_dir($dir)) {
return mkdir($dir, 0755, true);
}
return TRUE;
}


9、删除目录

/**
+----------------------------------------------------------
* 删除指定目录下的所有文件及目录
+----------------------------------------------------------
* @access public
* @param dirName 目录路径
* @return boolean
+----------------------------------------------------------
*/
public function removeDir($dirName)
{
$handle = opendir("$dirName");
if (!$handle) return FALSE;
while (false !== ($item = readdir($handle))) {
if ($item != "." && $item != ".."){
if(is_dir("$dirName/$item")){
$this->removeDir("$dirName/$item");
}else{
unlink("$dirName/$item");
}
}
}
closedir($handle);
return rmdir($dirName);
}


10、生成随机文件名

/**
+----------------------------------------------------------
* 創建文件名
+----------------------------------------------------------
* @access private
* @return filename
+----------------------------------------------------------
*/
public function generalFileName()
{
$now = microtime_format();
return date('YmdHis', $now[0]) . $now[1];
}


11、验证文件类型

/**
+----------------------------------------------------------
* 驗證文件類型
+----------------------------------------------------------
* @access private
* @param fileType string 文件後綴
* @return boolean
+----------------------------------------------------------
*/
public function checkFileType($fileType, $allowType)
{
return in_array(strtolower($fileType), $allowType);
}


12、解压缩压缩包

/**
+----------------------------------------------------------
* 解压缩zip包
+----------------------------------------------------------
* @access public
* @return boolean
+----------------------------------------------------------
*/
private function unzip($path)
{
$zip = new ZipArchive();
if ($zip->open($path) === FALSE) return FALSE;
$zip->extractTo(substr($path, 0, -4));
$zip->close();
return TRUE;
}



/**
+----------------------------------------------------------
* 解压缩zip包 linux 命令
+----------------------------------------------------------
* @access public
* @return boolean
+----------------------------------------------------------
*/
private function unzip_linux($path)
{
$target = substr($path, 0, strlen($path)-4);
exec("unzip -o $path -d $target");
return TRUE;
}


13、创建压缩包

/**
+----------------------------------------------------------
* 生成压缩包
+----------------------------------------------------------
* @access public
* @return boolean
+----------------------------------------------------------
*/
private function createZip($dirName)
{
$zip = new ZipArchive();
$filename = $dirName . ".zip";
if(file_exists($filename)) unlink($filename);
if (!$zip->open($filename, ZIPARCHIVE::CREATE))
return "create zip file failed";
$fileArr = $this->recursiveFiles($dirName);
foreach ($fileArr as $key => $value) {
$zip->addFile($value['path'], $value['parentFolder'] . $value['fileName']);
}
$zip->close();
return TRUE;
}

/**
+----------------------------------------------------------
* 通过linux命令生成压缩包
+----------------------------------------------------------
* @access public
* @return boolean
+----------------------------------------------------------
*/
private function createZip_linux($fileName, $directory, $dirName)
{
$zipFile = $fileName . ".zip";
$currPath = getcwd();
if (!chdir($dirName)) return FALSE; // 跳转到上传目录
exec("zip -q -m -r $zipFile *");
if (!chdir($currPath)) return FALSE; // 返回项目根目录

// 将生成的zip文件放到上一层目录
$source = $dirName . '/' . $zipFile;
$target = C("PAGE_ROOT") . C('ACTIVE_VOLUME') . $directory . $zipFile;
exec("mv -f $source $target");
return TRUE;
}


14、复制文件夹

/**
+----------------------------------------------------------
* 复制文件夹
+----------------------------------------------------------
* @access public
* @param originalPath 原始文件路径
* @param destPath 目标文件路径
* @return boolean
+----------------------------------------------------------
*/
public function copyFolder($originalPath, $destPath)
{
if (is_dir($originalPath)) {
$fileArray = array();
D("File")->createDir($destPath);
$handle = opendir($originalPath);
while (false !== ($file = readdir($handle))) // 循环读取目录中的文件名并赋值给$file
{
if ($file != "." && $file != "..") // 排除当前路径和前一路径
{
if (is_dir($originalPath . "/" . $file)) {
$this->copyFolder($originalPath . "/" . $file, $destPath . "/" . $file); // 获取子目录下的文件
} else {
copy($originalPath . "/" . $file, $destPath . "/" . $file);
}
}
}
} else {
copy( $originalPath, $destPath );
}
}


15、验证EMAIL格式

public function validEmail($email)
{
if ($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
return TRUE;
}else{
return FALSE;
}
} else {
return FALSE;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值