一些php常用函数积累

本文链接
<?php
// id: ecffe70d3af54df9bad97b61918ace7d global $ct\_path, $ct_log_path;
$log_path = "test_php.txt";
// 是否先log到buffer,再通过CT_flush()一次性写入文件 $ct_log_buffer = true;
$CT_off = true;
$request_num = _uniqid_();
$CT_format = "";

if ($ct_path) {

$dir = _dirname_($ct\_path);
**if** (!_file\_exists_($dir)) {
    _mkdir_($dir, 0777, **true**);
}
$file = _fopen_($ct\_path, **"a+"**);
$file\_ct\_log = $file;

}
if ($ct_log_path) {

$dir = _dirname_($ct\_log\_path);
**if** (!_file\_exists_($dir)) {
    _mkdir_($dir, 0777, **true**);
}
$file\_ct\_log = _fopen_($ct\_log\_path, **"a+"**);

}

$ct_buffer = [];

$path_my = __DIR__ . "/common.my.php";
if (_is_file_($path_my)) {

**require** $path\_my;

}

function clear_log() {
Global $log_path;
_unlink_($log_path);
}

function clog($content, $with_lf = true) {
Global $log_path;
$file = _fopen_($log_path,"a+");
_fwrite_($file, $content);
CT_log($content);
if($with_lf) {

  _fwrite_($file, **"\\n"**);
  CT\_log(**"\\n"**);

}
_fclose_($file);

}

function get_safe($obj, $key, $def = NULL) {
if(isset($obj\[$key])) {

  **return** $obj\[$key\];

}
return $def;
}

function get_stack_trace($title = "") {
$html = **"=================stack trace:"**.$title."\n";
$array =_debug_backtrace_(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach($array **as** $row) {

  $html .= _sprintf_(**"file:%s, line:%d, class:%s, function:%s\\n"**,
     get\_safe($row, **'file'**), get\_safe($row, **'line'**), get\_safe($row, **'class'**), get\_safe($row, **'function'**));

}
return $html;
}

function log_stack_trace($title = "") {
clog(get_stack_trace($title));
}

_// error handler function with stack trace.
// use like this:
// $old\_error\_handler = set\_error\_handler("err\_handler");_ **function** err\_handler($errno, $errstr, $errfile, $errline)
{
$errno_map = array(1 => "E_ERROR", 2 => "E_WARNING", 4 => "E_PARSE", 8 => "E_NOTICE",

  16 => **"E\_CORE\_ERROR"**, 32 => **"E\_CORE\_WARNING"**, 64 => **"E\_COMPILE\_ERROR"**,
  128 => **"E\_COMPILE\_WARNING"**, 256 => **"E\_USER\_ERROR"**, 512 => **"E\_USER\_WARNING"**, 
  1024 => **"E\_USER\_NOTICE"**, 2048 => **"E\_STRICT"**, 4096 => **"E\_RECOVERABLE\_ERROR"**,
  8192 => **"E\_DEPRECATED"**, 16384 => **"E\_USER\_DEPRECATED"**, 32767 => **"E\_ALL"**);

clog(_sprintf_("------------ %s(%d), msg:%s", $errno\_map\[$errno], $errno, $errstr));
log_stack_trace("");
/* Don't execute PHP internal error handler */ return true;
}

// 获取当前系统时间,返回float格式,单位:秒 function get_time() {
_date_default_timezone_set_('Asia/Shanghai');
list($usec, $sec) = _explode_(" ", _microtime_());
return ((float)$usec + (float)$sec);
}

function get_prefix() {

**return ""**;
**Global** $ip, $pid;
**if**(!**isset**($ip)) {
    $pid = _getmypid_();
    $ip = $\_SERVER\[**'REMOTE\_ADDR'**\];
}
**return** $pid.**' '**.$ip.**' '**._date_(**"m-d H:i:s "**);

}

function CT($content) {

**Global** $CT\_off, $file;
**if**($CT\_off || !$file)
    **return**;

**Global** $last\_time, $first\_time, $is\_first, $ct\_log\_buffer, $ct\_buffer, $request\_num, $CT\_format;
**if** ($CT\_format == **"raw"**) {
    $all\_out = $content . **"\\n"**;
} **else** {
    _// 通过stack trace计算缩进_ $array =_debug\_backtrace_(**_DEBUG\_BACKTRACE\_IGNORE\_ARGS_**);
    $ignore\_count = 0;
    $count = _count_($array);
    $ignore\_names = \[**"call\_user\_func\_array"**, **"call\_user\_func"**, **"spl\_autoload\_call"**\];
    $ignore\_classes = \[**"ReflectionClass"**\];
    **for** ($i = 2; $i < $count; $i++) {
        $frame = $array\[$i\];
        **if** (_in\_array_($frame\[**"function"**\], $ignore\_names) || **isset**($frame\[**"class"**\]) && _in\_array_($frame\[**"class"**\], $ignore\_classes)) {
            $ignore\_count++;
        }
    }
    $all\_out = get\_prefix() . _str\_pad_(**""**, $count - 2 - $ignore\_count, **" "**) . $request\_num . **" "** . $content;
    $cur\_time=get\_time();
    **if**(!$is\_first) {
        $is\_first = **true**;
        $last\_time = $first\_time = $cur\_time;
    }
    $total\_time=$cur\_time-$first\_time;
    $delta\_time=$cur\_time-$last\_time;
    $overtime\_flag = **""**;
    _// 添加超时标记_ **if**($delta\_time \* 1000 > 10)
        $overtime\_flag = **"----overtime"**;

    $all\_out = $all\_out.**" cur\_time:** $cur\_time**, total\_time:** $total\_time**, delta\_time:** $delta\_time $overtime\_flag**\\n"**;
    $last\_time=$cur\_time;
}

**if** ($ct\_log\_buffer === **true**) {
    $ct\_buffer\[\] = $all\_out;
} **else** {
    _fwrite_($file, $all\_out);
}

}

_/**
* 将buffer的CT内容写入文件
*_ @param _boolean turn_off_buffer, 完成后是否关闭buffer,以保证通过register_shutdown_function等调用的函数能够被输出
*/_ function CT_flush($turn_off_buffer)
{

**global** $file, $ct\_buffer, $ct\_log\_buffer;
**if** (!$file) {
    **return**;
}
_fwrite_($file, _join_(**""**, $ct\_buffer));
$ct\_buffer = \[\];
$ct\_log\_buffer = !$turn\_off\_buffer;

}

function CT_log($content = **""**, $path = NULL) {

**Global** $file\_ct\_log;
**if** (!$file\_ct\_log && !$path) {
    **return**;
}
$content = get\_prefix()._print\_r_($content, **true**).**"\\n"**;
**if**($path) {
    file\_create\_path($path);
    $file = _fopen_($path, **"a+"**);
    **if**($file) {
        _fwrite_($file, $content);
        _fclose_($file);
    }
}
**else** {
    _fwrite_($file\_ct\_log, $content);
}

}

_/**
* 日志输出,使用info level
*_ @param _$content
*_ @param _array $params
*_ @param _string $logger
*/_ function slog($content, $params = [], $logger = "default")
{

SeasLog::_info_($content, $params, $logger);

}

_/**
* 日志输出,使用debug level
*_ @param _$content
*_ @param _array $params
*_ @param _string $logger
*/_ function slog_debug($content, $params = [], $logger = "default")
{

SeasLog::_debug_($content, $params, $logger);

}

_/**
* 日志输出,使用error level
*_ @param _$content
*_ @param _array $params
*_ @param _string $logger
*/_ function slog_error($content, $params = [], $logger = "default")
{

SeasLog::_error_($content, $params, $logger);

}

_/**
* 日志输出,使用warning level
*_ @param _$content
*_ @param _array $params
*_ @param _string $logger
*/_ function slog_warning($content, $params = [], $logger = "default")
{

SeasLog::_warning_($content, $params, $logger);

}

_/*

获取指定的http response header 值。
eg:
HTTP/1.1 200 OK
Server: Tengine/2.1.2
Date: Sun, 02 Apr 2017 02:49:34 GMT
Content-Type: text/html; charset=gb2312
Content-Length: 124378
Connection: keep-alive
Cache-Control: private
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319

*/_ function curl_get_header($ch, $response, $key)
{

$header\_size = _curl\_getinfo_($ch, **_CURLINFO\_HEADER\_SIZE_**);
$header = _substr_($response, 0, $header\_size);
_// header processing_ $header\_arr = _explode_(**"\\r\\n"**, $header);

$value = **""**;
$key .= **":"**;
**foreach** ($header\_arr **as** $entry) {
    **if** (!_strncmp_($entry, $key, _strlen_($key))) {
        $value = _trim_(_substr_($entry, _strlen_($key)));
        **break**;
    }
}
**return** $value;

}

// 获取response状态码 function curl_get_status($ch, $response)
{

$header\_size = _curl\_getinfo_($ch, **_CURLINFO\_HEADER\_SIZE_**);
$header = _substr_($response, 0, $header\_size);
$header\_arr = _explode_(**"\\r\\n"**, $header);
**return** _explode_(**" "**, $header\_arr\[0\])\[1\];

}

function startsWith($haystack, $needle)
{

_// search backwards starting from haystack length characters from the end_ **return** $needle === **""** || _strrpos_($haystack, $needle, -_strlen_($haystack)) !== **FALSE**;

}

// http 301 is handled. function http_get_core($url, &$status = null)
{

CT\_log(**"-----------http\_get:"** . $url);
$ch = _curl\_init_();
$status = -1;

_// 301 最多嵌套3次。_ **for** ($i = 0; $i < 3; $i++) {
    $options = **array**(
        **_CURLOPT\_HEADER_** \=> 1,
        **_CURLOPT\_POST_** \=> 0,            _// 请求方式为POST_ **_CURLOPT\_URL_** \=> $url,      _// 请求URL_ **_CURLOPT\_RETURNTRANSFER_** \=> 1,  _// 获取请求结果_ **_CURLOPT\_TIMEOUT\_MS_** \=> 30000,    _// 超时时间(ms)_ **_CURLOPT\_POSTFIELDS_** \=> _http\_build\_query_(**array**()), _// 注入接口参数_ **_CURLOPT\_SSL\_VERIFYPEER_** \=> 0,  _// 不验证证书_ );
    _curl\_setopt\_array_($ch, $options);
    _curl\_setopt_($ch, **_CURLOPT\_ENCODING_**, **"gzip,deflate"**); _// 百度不支持
    curl\_setopt_($ch, **_CURLOPT\_CUSTOMREQUEST_**, **'GET'**);

    **if** (($response = _curl\_exec_($ch))) {
        _// 有的网站header、<head>指定的编码不一致,会导致乱码。因此如果有编码信息,将其转送到client。_ $content\_type = curl\_get\_header($ch, $response, **"Content-Type"**);
        **if**($content\_type) {
            _header_(**"Content-Type: "** . $content\_type);
        }

        $header\_size = _curl\_getinfo_($ch, **_CURLINFO\_HEADER\_SIZE_**);
        $status = $code = curl\_get\_status($ch, $response);
        **if** ($code == 301 || $code == 302) {
            $redirect\_url = curl\_get\_header($ch, $response, **"Location"**);
            $parsed\_re = _parse\_url_($redirect\_url);
            **if**(**isset**($parsed\_re\[**"host"**\])) {
                $url = $redirect\_url;
            }
            **else** {
                $parsed = _parse\_url_($url);
                **if**(startsWith($redirect\_url, **"/"**)) {
                    $url = $parsed\[**"schema"**\]. **"://"** . $parsed\[**"host"**\] . $redirect\_url;
                }
                **else** {
                    _//_ **_TODO 相对路径拼接_** $content =  **"relative path TODO"**;
                    _curl\_close_($ch);
                    **return** $content;
                }
            }
            **continue**;
        } **else if** ($code == 404) {
            _header_(**"Status: 404 Not Found"**);
            $msg = **array**(
                **"status"** \=> 404
            );
            $content = **"<p id='http\_util\_message\_block' style='display: none'>"** . _json\_encode_($msg) . **"</p>"**;
            $content .= **"404, not found!"**;
        } **else if** ($code == 200) {
            $content = _substr_($response, $header\_size);
        } **else** {
            $content = **"http error, code="** . $code . **"\\n"** . _substr_($response, $header\_size);
        }
    } **else** {
        $msg = **array**(
            **"status"** \=> -1
        );
        $content = **"<p id='http\_util\_message\_block' style='display: none'>"** . _json\_encode_($msg) . **"</p>"**;
        $content .= **"invoke error\["** . _curl\_error_($ch) . **"\]"**;
    }
    _curl\_close_($ch);
    **return** $content;
}

}

// 递归为path创建必要的路径 function file_create_path($path)
{

$dir = _dirname_($path);
**if** ($dir && !_file\_exists_($dir)) {
    _mkdir_($dir, 0755, **true**);
}

}

_// $save\_path 需要gbk编码_ **function** file\_save($content, $save\_path, $append)
{

file\_create\_path($save\_path);
**if** ($append) {
    $file = _fopen_($save\_path, **"a+"**);
    **if** ($file) {
        _fwrite_($file, $content);
        _fclose_($file);
    }
} **else** {
    _file\_put\_contents_($save\_path, $content);
}

}

// 默认的curl封装 function curl_do($url, $close_after_use = true)
{

CT\_log(**"curl\_do: "** . $url);
$ch = _curl\_init_();

$options = **array**(
    **_CURLOPT\_HEADER_** \=> 0,
    **_CURLOPT\_POST_** \=> 0,            _// 请求方式为POST_ **_CURLOPT\_URL_** \=> $url,      _// 请求URL_ **_CURLOPT\_RETURNTRANSFER_** \=> 1,  _// 获取请求结果_ **_CURLOPT\_TIMEOUT\_MS_** \=> 30000,    _// 超时时间(ms)_ **_CURLOPT\_POSTFIELDS_** \=> _http\_build\_query_(**array**()), _// 注入接口参数_ **_CURLOPT\_SSL\_VERIFYPEER_** \=> 0,  _// 不验证证书_ );

_curl\_setopt\_array_($ch, $options);
_curl\_setopt_($ch, **_CURLOPT\_ENCODING_**, **"gzip"**);
$response = _curl\_exec_($ch);
$err = _curl\_error_($ch);
**if** ($err) {
    CT\_log(**"curl error: "** . $err);
}
**if** ($close\_after\_use) {
    _curl\_close_($ch);
}
**return array**(**"handle"** \=> $ch, **"response"** \=> $response, **"err"** \=> $err);

}

_/**
* url拼接,没有处理user,pass两个components
* 详细定义参见单元测试
*_ @param _$url
*_ @param _$base
*_ @return _string
*/_ function get_absolute_url($url, $base)
{

_// 两种情况直接返回$url:_ **if** (!$base)
    **return** $url;
$url\_host = _parse\_url_($url, **_PHP\_URL\_HOST_**);
**if** ($url\_host) {
    **return** $url;
}

$base\_parsed = _parse\_url_($base);
$base\_scheme = get\_safe($base\_parsed, **"scheme"**, **""**);
$base\_host = get\_safe($base\_parsed, **"host"**, **""**);
$base\_port = **isset**($base\_parsed\[**"port"**\]) ? **":"** . $base\_parsed\[**"port"**\] : **""**;
$base\_path = get\_safe($base\_parsed, **"path"**);

**if** ($base\_host) {
    $base\_calc = $base\_scheme . **"://"** . $base\_host . $base\_port;
    **if** (startsWith($url, **"/"**)) {
        **return** $base\_calc . $url;
    } **else if** ($base\_path) {
        $pos = _strrpos_($base\_path, **"/"**);
        **if** ($pos !== **false**) {
            $dir = _substr_($base\_path, 0, $pos + 1); _// with last "/"_ **return** $base\_calc . $dir . $url;
        } **else** {
            **return** $base\_calc . **"/"** . $url;
        }
    } **else** {
        **return** $base\_calc . **"/"** . $url;
    }
} **else** {
    **if** (startsWith($url, **"/"**)) {
        **return** $url;
    } **else if** ($base\_path) {
        $pos = _strrpos_($base\_path, **"/"**);
        **if** ($pos !== **false**) {
            $dir = _substr_($base\_path, 0, $pos + 1); _// with last "/"_ **return** $dir . $url;
        } **else** {
            **return** $url;
        }
    } **else** {
        **return** $url;
    }
}

}

function get_absolute_url_tests()
{

_// empty tests_ $tests\[\] = \[**null**, **null**, **null**\];
$tests\[\] = \[**""**, **null**, **""**\];
$tests\[\] = \[**null**, **""**, **null**\];
$tests\[\] = \[**""**, **""**, **""**\];

$tests\[\] = \[**null**, **"/"**, **"/"**\];
$tests\[\] = \[**null**, **"/a"**, **"/"**\];
$tests\[\] = \[**null**, **"/a/"**, **"/a/"**\];

$tests\[\] = \[**"a.html"**, **"b"**, **"a.html"**\];
$tests\[\] = \[**"a.html"**, **""**, **"a.html"**\];
$tests\[\] = \[**"a.html"**, **"/"**, **"/a.html"**\];
$tests\[\] = \[**"a/b/c/a.html"**, **"http://1.1.1.1"**, **"http://1.1.1.1/a/b/c/a.html"**\];
$tests\[\] = \[**"a.html"**, **"http://1.1.1.1:83"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"a.html"**, **"http://1.1.1.1:83/"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"a.html"**, **"http://1.1.1.1:83/a/b"**, **"http://1.1.1.1:83/a/a.html"**\];
$tests\[\] = \[**"a.html"**, **"http://1.1.1.1:83/?"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"a.html"**, **"http://1.1.1.1:83?"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"a.html"**, **"http://1.1.1.1:83?a=b"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"a.html"**, **"https://1.1.1.1:83?a=b#1"**, **"https://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"a.html"**, **"www.baidu.com?a=b#1"**, **"a.html"**\]; _// www被认为是path

// starts with "/"_ $tests\[\] = \[**"/a.html"**, **"b"**, **"/a.html"**\];
$tests\[\] = \[**"/a.html"**, **""**, **"/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"/"**, **"/a.html"**\];
$tests\[\] = \[**"/a/b/c/a.html"**, **"http://1.1.1.1"**, **"http://1.1.1.1/a/b/c/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"http://1.1.1.1:83"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"http://1.1.1.1:83/"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"http://1.1.1.1:83/a/b"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"http://1.1.1.1:83/?"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"http://1.1.1.1:83?"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"http://1.1.1.1:83?a=b"**, **"http://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"https://1.1.1.1:83?a=b#1"**, **"https://1.1.1.1:83/a.html"**\];
$tests\[\] = \[**"/a.html"**, **"www.baidu.com?a=b#1"**, **"/a.html"**\]; _// www被认为是path_ $r = **true**;
**foreach** ($tests **as** $test) {
    $abs = get\_absolute\_url($test\[0\], $test\[1\]);
    **echo** $abs . **"\\n"**;
    **if** ($abs !== $test\[2\]) {
        $r = **false**;
        **break**;
    }
}
**echo "pass: "** . $r . **"\\n"**;

_// return self tests_ $tests\_self = \[\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"b"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **""**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"/"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a/b/c/a.html"**, **"http://1.1.1.1"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"http://1.1.1.1:83"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"http://1.1.1.1:83/"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"http://1.1.1.1:83/a/b"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"http://1.1.1.1:83/?"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"http://1.1.1.1:83?"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"http://1.1.1.1:83?a=b"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"https://1.1.1.1:83?a=b#1"**, **""**\];
$tests\_self\[\] = \[**"http://test.com/a.html"**, **"www.baidu.com?a=b#1"**, **""**\]; _// www被认为是path_ **echo "-------------------return self test--------:\\n"**;
$r = **true**;
**foreach** ($tests\_self **as** $test) {
    $abs = get\_absolute\_url($test\[0\], $test\[1\]);
    **echo** $abs . **"\\n"**;
    **if** ($abs !== $test\[0\]) {
        $r = **false**;
        **break**;
    }
}
**echo "pass: "** . $r . **"\\n"**;

}

_/**
* 路径规范化。
* eg:
* a/../b => b
* ../a/../../b => ../b
* ./a/../b => ./b
*_ @param _$path
*_ @return _mixed|string
*/_ function normalize_path($path)
{

**if** (!$path) {
    **return** $path;
}
$path = _str\_replace_(**"\\\\"**, **"/"**, $path);

$dotdotCount = 0; _// 以".."开头的处理_ $arr = _explode_(**"/"**, $path);
$pathStack = \[\];
**foreach** ($arr **as** $ele) {
    **if** ($ele !== **".."**) {
        _array\_push_($pathStack, $ele);
    } **else** {
        **if** (_count_($pathStack) === 0) {
            $dotdotCount++;
        } **else** {
            _array\_pop_($pathStack);
        }
    }
}
**unset**($ele);

$r = _str\_pad_(**""**, $dotdotCount, **"../"**);
**foreach** ($pathStack **as** $path) {
    **if** ($r === **""**) {
        $r = $path;
    } **else** {
        $r .= **"/"** . $path;
    }
}
**return** $r;

}

// 尝试gbk、utf-8两种编码;优先尝试传入编码 function is_file_ex($path)
{

**if** (_is\_file_($path)) {
    **return true**;
}
$enc = _mb\_detect\_encoding_($path, **"gb2312"**, **true**);
**if** ($enc === **'EUC-CN'**) {
    $path2 = _iconv_(**"gbk"**, **"utf-8"**, $path);
} **else** {
    $path2 = _iconv_(**"utf-8"**, **"gbk"**, $path);
}
**return** _is\_file_($path2);

}

// 尝试gbk、utf-8两种编码;优先尝试传入编码 function file_get_contents_ex($path)
{

**if** (_is\_file_($path)) {
    **return** _file\_get\_contents_($path);
}
$enc = _mb\_detect\_encoding_($path, **"gb2312"**, **true**);
**if** ($enc === **'EUC-CN'**) {
    $path2 = _iconv_(**"gbk"**, **"utf-8"**, $path);
} **else** {
    $path2 = _iconv_(**"utf-8"**, **"gbk"**, $path);
}
**if** (_is\_file_($path2)) {
    **return** _file\_get\_contents_($path2);
}
**return false**;

}

// 创建文件lock,如果路径不存在则创建之 function create_file_lock($path, &$output = null)
{

file\_create\_path($path);
$f = **null**;
_// 不使用"@",这样忽略文件存在的报错,其他异常返回(如权限问题)_ **try** {
    $f = _fopen_($path, **"x"**);
} **catch** (Exception $e) {
    $msg = $e->getMessage();
    **if** (_strpos_($msg, **"File exists"**) === **false** && _strpos_($msg, **"文件已存在"**) === **false**) {
        $output = $e->getMessage() . **"\\n"** . $e->getTraceAsString();
    }
}
**return** $f;

}

// 关闭、释放文件lock function release_file_lock($f, $path)
{

**if** ($f) {
    _fclose_($f);
    _unlink_($path);
}

}

_/**
* echo udate('Y-m-d H:i:s.u T');
*_ @param _string $format
*_ @param _null $utimestamp
*_ @return _false|string
*/_ function udate ($format = **'u'**, $utimestamp = null)
{

**if** (_is\_null_($utimestamp))
    $utimestamp = _microtime_(**true**);

$timestamp = _floor_($utimestamp);
$milliseconds = _round_(($utimestamp - $timestamp) \* 1000000);

**return** _date_(_preg\_replace_(**'\`(?<!\\\\\\\\)u\`'**, $milliseconds, $format), $timestamp);

}
?>

评论

暂无评论