设为首页 收藏本站
查看: 625|回复: 0

[经验分享] PHP常用函数收集

[复制链接]

尚未签到

发表于 2017-3-30 11:55:05 | 显示全部楼层 |阅读模式
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;
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-357615-1-1.html 上篇帖子: php转asicc码函数 下篇帖子: PHP 计算字符串长度
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表