php常用自定义函数类下载
https://pan.baidu.com/s/18ZuQm5cx3ynQtQqcQgK0Vw
php 设置字符编码为utf-8
header("Content-Type: text/html;charset=utf-8");
GB2312和utf8相互转换
echo iconv('GB2312', 'UTF-8//IGNORE', $str);
路径格式化(替换双斜线为单斜线)
function path_formate($str){
return str_replace('\\\\','\\',$str);
}
获取当前文件的目录
方法一:
define('BASE_PATH',rtrim(str_replace('\\','/',__DIR__).'/'));
方法二:
define('BASE_DIR', rtrim(str_replace('\\', '/', dirname(__FILE__)),'/').'/');
打印输出
function dump($var)
{
echo '<pre>';
print_r($var);
echo '</pre>';
}
function vp($var)
{
echo '<pre>';
var_dump($var);
echo '</pre>';
}
function dd($var)
{
echo '<pre>';
var_dump($var);
echo '</pre>';
die;
}
api返回信息
字符串截取
方法一:
在公共的common.php中
在模版中调用则:
{$tops.title | subtext=18}
方法二:
在公共的common.php中
在模版中调用则:
{$brand.brand_description|cut_str=###,10}
字符串make_by_id转成makeById
function changestr($string){
if(!is_string($string)){
die('please input string.');
}
if(strpos($string,'_')){
$stringArr=explode('_',$string);
$str='';
foreach ($stringArr as $v){
$str .= ucfirst($v);
}
return $str;
}
}
数组 字符串 对象 json格式的字符串互转
强制类型转换

php序列化serialize与返回序列化unserialeze
serialize() 把变量和它们的值编码成文本
unserialize() 恢复原先变量
创建日志文件
- 方法一(适合临时用):
- 方法二:
例: /www.wdmcake.com/data/log/日期/$file文件名
- 方法三:
function create_log($data,$merchant,$agency,$file='')
{
if(empty($merchant)) die("商户code不可为空");
if(empty($agency)) die("平台code不可为空");
$file_path = ROOT_PATH . 'logdata/'.$merchant.'/'.$agency;
if(!is_dir($file_path)){
mkdir($file_path, 0777, true);
}
file_put_contents($file_path.'/'.$agency.'_' . $file . date('Ymd',time()) . '.log', date('Y-m-d H:i:s',time()) .' ' . var_export($data,true) . "\r\n", FILE_APPEND);
}
返回二维数组其中一段的数据
例子:
<?php
header('Content-Type=text/html;charset:utf8');
$arr = array(
[
'id' => '1',
'name' => '李四',
'age' => '21',
'gender' => '男',
],
[
'id' => '2',
'name' => '王五',
'age' => '22',
'gender' => '男',
],
);
echo '<pre>';
print_r(slice($arr, 0, 2));
获取ip地址
- 方法一
$_SERVER['REMOTE_ADDR']
- 方法二
- 方法三
PHP限制IP访问 只允许指定IP访问 允许*号通配符过滤IP
获取地址路径里0到第一个\线的部分
function f_dirname($f_path){
return substr($f_path,0,strrpos($f_path,'\\'));
}
获取当前文件所在的文件路径
define('CUR_API_PATH', rtrim(str_replace('\\', '/', dirname(__FILE__)),'/').'/');
获取随机字符串
中文字符串反转

检查目标文件夹是否存在,如果不存在则自动创建该目录
默认获得文件修改时间
function filetime($way,$char='m'){
date_default_timezone_set('PRC');
switch($char){
case 'c':$localtime = date('Y-m-d H:i:s',filectime($way));
break;
case 'm':$localtime = date('Y-m-d H:i:s',filemtime($way));
break;
case 'a':$localtime = date('Y-m-d H:i:s',fileatime($way));
break;
}
return $localtime;
}
判断后缀类型
function suffixtype($f_path){
$info = pathinfo($f_path);
$f_type = 'file';
switch(strtolower(@$info["extension"])){
case 'jpg':case 'jpeg':case 'gif':
case 'png':case 'bmp':$f_type = 'image';break;
case 'pl':case 'c':case 'cpp':case 'log':case 'asp':case 'php':case 'jsp':case 'txt':case 'xml':case 'html':case 'htm':case 'phtml':case 'jhtml':case 'java':case 'cfg':case 'ini':
case 'text':case 'bat':$f_type = 'text';break;
}
return $f_type;
}
检查文件类型
获取文件后缀名,并判断是否在定义的数组中
判断路径是文件还是目录
function f_type($f_path){
return is_dir($f_path)?'dir':suffixtype($f_path);
}
计算文件或目录字节大小
接收异步过来的get消息
$sms_result_data = $_GET;
curl之get请求
function _curl_get($urldata)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$urldata);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl之post请求
如果是有图片要上传加下面用这行代码
curl_file_create(ROOT_PATH.'public'.$value["goods_img"]);
curl之post_get请求
如果是有图片要上传加下面用这行代码
curl_file_create(ROOT_PATH.'public'.$value["goods_img"]);
- 方法一
- 方法二
- 方法三
方法四**:
public $connectTimeout = 0;
public $readTimeout = 0;
public function curl($url, $postFields = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($this->readTimeout) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
}
if ($this->connectTimeout) {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
}
判断数组类型参数是否含有空元素值
判断是否传入必要参数