找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 81|回复: 2

一些函数

[复制链接]

86

主题

84

回帖

728

积分

管理员

积分
728
发表于 2023-8-14 11:50:33 | 显示全部楼层 |阅读模式
本帖最后由 aloneship 于 2024-4-23 17:15 编辑

对象转数组

  1. //从res对象获取数组
  2. function getArrayFromRes($object, $is_list = true) {
  3.     if(isset($object)){
  4.         if($is_list){
  5.             $arr = $object->all();
  6.         }else{
  7.             $arr = $object;
  8.         }

  9.     }else{
  10.         $arr = [];
  11.     }

  12.     return objectToArray($arr);
  13. }

  14. //对象转数组
  15. function objectToArray($object) {
  16.     //先编码成json字符串,再解码成数组
  17.     return json_decode(json_encode($object), true);
  18. }
复制代码
写日志
1.
  1. //跟踪错误日志
  2. function traceErrorLog($e){
  3.     if(!file_exists('logs')){
  4.         @file_put_contents('log_error'.date("Ymd").'.log', date('Y-m-d H:i:s').'|'.$e->getFile().'|line:'.$e->getLine().'|'.$e->getMessage()."\r\n", FILE_APPEND);
  5.     }else{
  6.         @file_put_contents('logs/'.'log_error'.date("Ymd").'.log', date('Y-m-d H:i:s').'|'.$e->getFile().'|line:'.$e->getLine().'|'.$e->getMessage()."\r\n", FILE_APPEND);
  7.     }

  8.     return true;
  9. }
复制代码

2.
  1. //写调试日志
  2. function debugLog($data = array(), $file_name = 'logtest.log'){
  3.     try{
  4.         @file_put_contents('logs/'.$file_name, serialize($data)."\r\n", FILE_APPEND);
  5.     }catch(\Exception $e) {
  6.         @file_put_contents('logs/debugLog_error.log', $e->getFile().'|'.$e->getLine().'|'.$e->getMessage()."\r\n", FILE_APPEND);
  7.     }

  8. }
复制代码
  1. //判断是不是json
  2. function is_json($data = '', $assoc = false) {
  3.     $data = json_decode($data, $assoc);
  4.     if ($data && (is_object($data)) || (is_array($data) && !empty(current($data)))) {
  5.     return true;
  6.     }
  7.     return false;
  8. }

  9. //获取13位时间戳
  10. function get_total_millisecond() {
  11.     list($t1, $t2) = explode(' ', microtime());
  12.     return sprintf('%u', (floatval($t1) + floatval($t2)) * 1000);
  13. }
复制代码




TP3 表字段
  1.   /**
  2.      * 查看表结构
  3.      * @param $tableName //表名
  4.      * @return array|void|null
  5.      * @date 2023/08/11
  6.      * @author zlm
  7.      */
  8.     public function tableDetail($tableName)
  9.     {
  10.         try {
  11.             if (!$tableName) {
  12.                 return [];
  13.             }

  14.             // 获取表的字段信息
  15.             $columns = M()->query("SHOW FULL COLUMNS FROM $tableName");


  16.             $data = [];
  17.             foreach ($columns as $column) {
  18.                 $columnName = $column['field'];
  19.                 $columnComment = $column['comment'];
  20.                 $data[$columnName] = $columnComment;

  21.             }

  22.             return $data;
  23.         } catch (\Exception $e) {
  24.             dump($e);
  25.             //  traceErrorLog($e);
  26.         }
  27.     }
复制代码

86

主题

84

回帖

728

积分

管理员

积分
728
 楼主| 发表于 2024-5-24 13:43:03 | 显示全部楼层

  1. 函数:urldecode()

  2. 功能:本函数将 URL 编码后字符串还原成未编码的样子。编码使用 %## 的格式。

  3. 范例:
  4. <?php  
  5. echo urldecode("%3C%BF%AA%CA%BC%BA%E4%D5%A8%3E");
  6. ?>
复制代码

86

主题

84

回帖

728

积分

管理员

积分
728
 楼主| 发表于 2024-5-24 13:43:30 | 显示全部楼层

  1. array parse_url ( string $url )
  2. 本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分。

  3. 本函数不是用来验证给定 URL 的合法性的,只是将其分解为下面列出的部分。不完整的 URL 也被接受,parse_url() 会尝试尽量正确地将其解析。

  4. 范例:
  5. <?php
  6. $url = 'http://username:password@hostname/path?arg=value#anchor';
  7. print_r(parse_url($url));
  8. echo parse_url($url, PHP_URL_PATH);
  9. ?>
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Discuz! X

GMT+8, 2025-12-6 12:40 , Processed in 0.088250 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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