将文件转字符串
//将文件转字符串 $content = file_get_contents('./tmp.xlsx');
根据某几个键值相同就去重
$list = $newData = []; //word,type,style3个相同的去重 foreach ($data as $key => $value) { if ($key != 0) { $newData[$value[0]] = $value[1] . ',' . $value[2] . ',' . $value[3]; $list[$value[0]] = $value; } } //获取去掉重复数据的数组 $uniqueArr = array_unique($newData); //重复数组 $repeatArr = array_diff_assoc($newData, $uniqueArr); //删除重复 foreach ($repeatArr as $rekey => $revalue) { unset($list[$rekey]); } sort($list);
IdArr
删除空值(去重(获取某值变成一维数组)) $idArr = array_filter(array_unique(array_column($list, 'id')));
php 生成文件txt到指定目录
file_put_contents("c:/zll.txt","内容");
php.ini设置上传临时文件路径
upload_tmp_dir = D:\owen\lswj\
基于php实现base64格式的图片数据如何转成图片
// $base_img是获取到前端传递的值 $base_img = str_replace('data:image/jpg;base64,', '', $code); // 设置文件路径和命名文件名称 $path = "D:/Uploads/Bar/2019-12-11/"; $output_file = time().rand(100,999).'.jpg'; $path = $path.$output_file; // 创建将数据流文件写入我们创建的文件内容中 file_put_contents($path, base64_decode($base_img)); // 输出文件 print_r($output_file);
php自定义返回json数据函数
public function result($errno, $message, $data = '') { ob_clean();//清空输出缓冲区 exit(json_encode(array( 'errno' => $errno, 'message' => $message, 'data' => $data, ))); } return $this->result('10014', 'redis connect fail', '');
字符串
//存在某字符,替换字符 if(strpos($compareOdds,"\")!==false){ $compareOdds=str_replace("\",'',$compareOdds); }
//字符串是否存在某字符 strstr($data['tags'], ',') //字符转数组 $tagsArr = explode(", ", $data['tags']); /** * 实现姓名和手机号星号*替换 * @return string */ if (!function_exists('mbSubstrReplace')) { function mbSubstrReplace($string, $replacement, $start, $length = null, $encoding = null) { if (extension_loaded('mbstring') === true) { $string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding); if ($start < 0) { $start = max(0, $string_length + $start); } else if ($start > $string_length) { $start = $string_length; } if ($length < 0) { $length = max(0, $string_length - $start + $length); } else if ((is_null($length) === true) || ($length > $string_length)) { $length = $string_length; } if (($start + $length) > $string_length) { $length = $string_length - $start; } if (is_null($encoding) === true) { return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length); } return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding); } return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length); } } /** * 实现姓名*替换 * 马克思:马*思 * 马思:*思 * 马列主义:马*义 * @return string */ if (!function_exists('getMaskName')) { function getMaskName($name) { if (mb_strlen($name) > 2) { return mbSubstrReplace($name, '*', 1, -1); } else { return mbSubstrReplace($name, '*', 0, -1); } } } /** * 实现手机号星号*替换 * 12345679878:123****9878 * @return string */ if (!function_exists('getMaskMobile')) { function getMaskMobile($mobile) { return substr($tel, 0, 3)."****".substr($tel, 7); } }
PHP 对象数组和一般的数组的相互转化
/* * 把数组转化成任何你想要的对象类型的数组: */
if (!function_exists('arrayToObject')) { function arrayToObject($array) { $aboutVideoArr = array(); if (is_array($array)) { foreach ($array as $value) { $video = new Video(); foreach ($value as $key => $val) { $video->$key = $val; } array_push($aboutVideoArr, $video); } } else { $video = $array; } return $aboutVideoArr; } } // 把对象转化成普通的数组: if (!function_exists('objectToarray')) { function objectToarray($object) { if (is_object($object)) { foreach ($object as $key => $value) { $array[$key] = $value; } } else { $array = $object; } return $array; } } /** * 生成一批随机数 随机数之合为一个值 * @param $count 随机数个数 * @param $sum 随机数总和 * @param $mix 最小随机数 * @param $max 最大随机数 */ public function getRand($count, $sum, $mix, $max) { ini_set('memory_limit', "1024M"); $i = 1; while ($i <= $count) { $tmp[] = mt_rand($mix, $max); $i++; } $k = array_sum($tmp) ? ($sum / array_sum($tmp)) : 0; $res = []; foreach ($tmp as $v) { $res[] = (int)($v * $k); } $assoc = $sum - array_sum($res); $last = array_pop($res); $res[] = $last + $assoc; while (max($res) > $max) { $tmax = max($res); $key = array_search($tmax, $res); if ($key !== false) { $res[$key] = mt_rand($mix, $max); $k = array_search(min($res), $res); $res[$k] = $tmax - $res[$key] + min($res); } } return $res; }
php 数组进行分页
$pagesize = input('limit/d', 20); $page = input('page/d', 1); $createTimeArr = getBetweenTimeArr($createTimeStart, date('Y-m-d')); $count = count($createTimeArr); $start = ($page - 1) * $pagesize; $end = $start + $pagesize; $end = $end > $count ? $count : $end; $newList = []; for ($i = $start; $i < $end; $i++) { $newList[] = $createTimeArr[$i]; } return $newList;
php获取2个时间内的所有日期
/**
- 获取2个时间内的所有日期
- @access public
- @param string $startDate 开始时间2019-12-30
- @param string $backDate 结束时间2020-01-02
- @return array 返回类型
- array(4) {
- [0]=>
- string(10) "2019-12-30"
- [1]=>
- string(10) "2019-12-31"
- [2]=>
- string(10) "2020-01-01"
- [3]=>
- string(10) "2020-01-02"
- } */
function getBetweenTimeArr($startDate, $backDate) { if (!$startDate || !$backDate){ return []; } $dtStart = strtotime($startDate); $dt_end = strtotime($backDate); $getBetweenTimeArr = []; while ($dtStart <= $dt_end) { $getBetweenTimeArr[] = date('Y-m-d', $dtStart); $dtStart = strtotime('+1 day', $dtStart); } return $getBetweenTimeArr; }
//2个时间相差几天$start_date 开始时间2019-12-30 $back_date 结束时间2020-01-02 $dimdd = round((strtotime($back_date)-strtotime($start_date))/3600/24)+1;
/**
- Describe:截取某子字符串前的字符串
- @param $string 输入字符串
- @param $beforeString 截取前字符串
- @return string 截取某子字符串前的字符串
- Created by zhangzq at 2021/4/26 17:23 */
function getBeforeString($string, $beforeString) { return substr($string, 0, strpos($string, $beforeString)); } // 截取指定两个字符之间字符串 if (!function_exists('getBetweenString')) { function getBetweenString($string, $startString, $endString) { $substr = substr($string, strlen($startString) + strpos($string, $startString), (strlen($string) - strpos($string, $endString)) * (-1)); return $substr; } } // 截取某子字符串后的字符串 if (!function_exists('getLaterString')) { function getLaterString($string, $laterString) { return substr($string, (stripos($string, $laterString) + strlen($laterString))); } }
//第一个字符是否为“ if (substr($compareOdds, 0, 1) == '"') { //删除第一个字符 $compareOdds = substr($compareOdds, 1); } //最后一个字符是否为“ if (substr($compareOdds, -1, 1) == '"') { //删除最后一个字符 $compareOdds = substr($compareOdds, 0, -1); }