PHP 常用函数 PHP

1.ceil() 函数向上舍入为最接近的整数。(例:获取总页数)
2.strtolower() 该函数将传入的字符串参数所有的字符都转换成小写,并以小定形式放回这个字
3.strtoupper() 该函数的作用同strtolower函数相反,是将传入的字符参数的字符全部转换成大
4.trim($h,',') 移除字符串两侧的字符
5.array_rand($array,$number)从数组中随机选出一个或多个元素,并返回。
6.abs()函数的作用是返回一个数的绝对值。语法是abs(number),如果参数 number 是 float,则返回的类型也是 float,否则返回 integer(因为 float 通常比 integer 有更大的取值范围)。
7.floor()函数用来向下舍入为最接近的整数。语法是floor(number),表示返回不大于参数number的下一个整数,有小数部分则直接舍去取整。
8.number_format()函数表示通过千位分组来格式化数字,语法是number_format(number,decimals,decimalpoint,separator),该函数支持一个,两个或四个参数(不是三个)。如果除了参数number外未设置其他参数,则数字会被格式化为不带小数点且以逗号(,)作为千位分隔符。
9.str_shuffle()函数用于随机的打乱字符串中所有字符,语法是str_shuffle(string),返回已打乱的字符串。
10.wordwrap()函数表示按照指定长度对字符串进行折行处理,语法是wordwrap(string,width,break,cut),若函数成功执行,则返回折行后的字符串,如果失败,则返回false。
11.strrev()函数用于反转字符串,语法是strrev(string),返回已反转的字符串。
12.str_split()函数用于把字符串分割到数组中,语法是str_split(string,length),将字符串按规定长度分割组成数组。
13.str_repeat()函数用于重复使用指定字符串,语法是str_repeat(string,repeat),此函数把字符串重复指定的次数。
14.str_pad()函数用于把字符串填充到指定长度,语法是str_pad(string,length,pad_string,pad_type),把字符串填充为指定的长度。
15.is_file()函数用于判断给定文件名是否为一个正常的文件,语法是bool is_file ( string $filename ),如果文件存在且为正常的文件则返回 TRUE,否则返回 FALSE。
16.ltrim()函数用于删除字符串左边的空格或其他预定义字符,语法是ltrim(string,charlist),返回经过charlist规则处理后的字符串
17.rtrim()函数用于删除字符串右边的空格或其他预定义字符,语法是rtrim(string,charlist),返回经过charlist规则处理后的字符串
18.pi()函数用于获取圆周率值,语法是pi(),这个函数只是单纯的用来获取圆周率值
19.rand()函数表示从参数范围内得到一个随机数,语法是rand(X,Y),从两个参数范围内得到一个随机数,随机数大于等于X或者小于等于Y
20.mt_rand()函数表示从参数范围内得到一个随机数,语法是mt_rand(X,Y),从两个参数范围内得到一个随机数,随机数大于等于X或者小于等于Y 和rand相似,速度比rand快 可搭配mt_getrandmax使用
21.min()从所有参数中找到最小数 min(X,Y,Z) 或者min(array(X,Y,Z))
22.max()从所有参数中找到最大数 max(X,Y,Z) 或者max(array(X,Y,Z))
23.sqrt()函数的作用是对参数进行求平方根 sqrt(9) = 3
24.round()函数的作用是对浮点数进行四舍五入
25.pow()函数的作用是将一个数进行n次方计算后返回 pow(5,3)=5*5*5=125
26.fmod()函数的作用是两个数值做除法运算后的余数 fmod(5,3)=2;
27.ucfirst() 字符串首字母大写
28.ucwords() 把每个单词的首字符转换为大写
29.htmlentities() 把字符转换为 HTML 实体
30.htmlspecialchars() 函数把预定义的字符转换为 HTML 实体。预定义的字符有:& (和号)成为 &," (双引号)成为 ",' (单引号)成为 ',< (小于)成为 <,> (大于)成为 >。
31.nl2br()在字符串中的新行(\n)之前插入换行符
32.strip_tags() 剥去字符串中的 HTML 标签
33.addcslashes() 返回在指定字符前添加反斜杠的字符串
34.stripcslashes() 删除由 addcslashes() 函数添加的反斜杠。
35.chr() 函数从指定的 ASCII 值返回字符。ASCII 值可被指定为十进制值、八进制值或十六进制值。八进制值被定义为带前置 0,而十六进制值被定义为带前置 0x。
36.ord() 返回字符串的首个字符的 ASCII 值。
37.strcasecmp()比较两个字符串(不区分大小写)==0 相同
38.strcmp() 比较两个字符串(区分大小写)
39.strncmp() 比较字符串前n个字符,区分大小写
40.strncasecmp() 比较字符串前n个字符,不区分大小写
41.strtok() 逐一分割字符串
42.explode() 把字符串打散为数组
43.implode() 把数组元素组合为字符串
44.substr() 截取字符串
45.str_replace() 字符串替换操作,区分大小写  (使用场景更多是对单个字符串和连续字符串过滤替换处理.   注意:间隔字符串过滤替换,更多使用php正则匹配替换处理  )
46.str_ireplace() 字符串替换操作,不区分大小写
47.substr_count() 统计一个字符串,在另一个字符串中出现次数
48.substr_replace() 替换字符串中某串为另一个字符串 (使用场景对字符串长度位置检索进行过滤替换或插入处理. 注意:对中文字符串截取不适用,更多使用中文截取函数mb_substr() )
49.similar_text() 计算两个字符串的相似度,并返回匹配字符的数目
50.strrchr 返回一个字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符
51.strchr()搜索字符串在另一字符串中的第一次出现。该函数是 strstr() 函数的别名。该函数是二进制安全的。该函数对大小写感敏。如需进行不区分大小写的搜索,请使用 stristr() 函数。

52.stristr() 返回一个字符串在另一个字符串中开始位置到结束的字符串,不区分大小写
53.strtr() 转换字符串中的某些字符
54.strpos() 寻找字符串中某字符最先出现的位置  区分大小写
55.stripos() 寻找字符串中某字符最先出现的位置 不区分大小写

杨佳乐 发布于  2024-4-30 11:13 

PHP 文件写入数组 PHP

文件写入函数

file_put_contents(file,data,mode,context)

参数 描述 file 必需。规定要写入数据的文件。如果文件不存在,则创建一个新文件。 data 可选。规定要写入文件的数据。可以是字符串、数组或数据流。 mode 可选。规定如何打开/写入文件。可能的值: FILE_USE_INCLUDE_PATH、FILE_APPEND、LOCK_EX 。context可选。规定文件句柄的环境。 context 是一套可以修改流的行为的选项。若使用 null,则忽略。

$arr = array(
    'name'=>'张三'
)
$str = var_export($arr,TRUE);
file_put_contents($filename,$str);

杨佳乐 发布于  2024-4-30 11:13 

PHP 处理日期、时间、时间戳相关操作 PHP

//时间相关
    public function timeinfo(){

        //获取今天0点-24点时间戳
        $today = strtotime(date('Y-m-d', time()));
        $todayend = $today + 24 * 60 * 60;

        //获取本周第一天及最后一天
        $weekfirst=strtotime(date('Y-m-d', strtotime('this week')));
        $weeklast=strtotime(date('Y-m-d', strtotime('last day next week')));

        //3.获取当天年份、月份、日及天数.
        echo " 本月共有:".date("t")."天";
        echo " 当前年份".date('Y');
        echo " 当前月份".date('m');
        echo " 当前几号".date('d');

        //1.获取上个月第一天及最后一天.
        echo date('Y-m-01', strtotime('-1 month'));
        echo "<br/>";
        echo date('Y-m-t', strtotime('-1 month'));

        //2016-08-10这天 2个月后的日期
        echo date("Y-m-d",strtotime("+2 month",strtotime("2016-08-10")));

        //获取3 年、月、日 后的日期
        echo date("Y-m-d",strtotime("+3 year",time()));
        echo date("Y-m-d",strtotime("+3 month",time()));
        echo date("Y-m-d",strtotime("+3 day",time()));

        //获取今天、昨天、明天的日期
        echo "今天:".date("Y-m-d")."<br>";       
        echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";       
        echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";    
        echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";       
        echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";       
        echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";       
        echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";       
        echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";       
        echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>";       
        echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";        

    }

    //获取当月第一天和最后一天
    function getthemonth($date)
    {
        $firstday = date('Y-m-01', strtotime(date('Y-m-d')));
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        return array($firstday,$lastday);
    }

    //计算两个时间戳相差天数
    function timediff($begin_time,$end_time)
    {
        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //计算天数
        $timediff = $endtime-$starttime;
        $days = intval($timediff/86400);
        //计算小时数
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        //计算分钟数
        $remain = $remain%3600;
        $mins = intval($remain/60);
        //计算秒数
        $secs = $remain%60;
        $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
        return $res;
    }

杨佳乐 发布于  2024-4-30 11:12 

PHP GET POST 请求接口 PHP

public function http_get_json($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
public function http_post_json($url,$data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Errno'.curl_error($ch);
    }
    curl_close($ch);
    $arr= json_decode($tmpInfo,true);
    return $arr;
}

杨佳乐 发布于  2024-4-30 11:09 

PHPExcel 导出 导入 PHP

//导出代码 亲测可用 需先在vendor 下引入PHPExcel
 public function order_dc(){
        if(IS_POST){
            //执行导出

            $where="1=1";

            //店铺
            if(!empty(I('spid'))){
                $where.=" and shopid='".I('spid')."'";
            }

            //处理时间
            if(!empty(I('start_time'))){
                $where.=" and createdat>".strtotime(I('start_time'));
            }

            if(!empty(I('end_time'))){
                $where.=" and createdat<".strtotime(I('end_time'));
            }

            $list=M('wm_orderlist')->where($where)->select();

            //excel导出
            vendor("PHPExcel.PHPExcel");

            vendor("PHPExcel.PHPExcel.IOFactory");
            vendor("PHPExcel.PHPExcel.Reader.Excel5");
            vendor("PHPExcel.PHPExcel.Reader.Excel2007");

            $objexcel=new \PHPExcel();

            //设置excel文件名称 当前日期
            $date = date("Y_m_d_h_i_s",time());
            $fileName = "{$date}.xlsx";

            //Excel文件的说明信息
            $objexcel->getProperties()->setCreator("Maarten Balliauw")
                ->setLastModifiedBy("Maarten Balliauw")
                ->setTitle("Office 2007 XLSX Test Document")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");

            $objexcel->setActiveSheetIndex(0)
                ->setCellValue('A1','test')
                ->setCellValue('B1','test');

            //把表中数据导入Excel文件中,多数据循环设置值   如果是长数字在前面加个空格变换成字符串

            foreach($list as $key=> $value) {
                $key+=2;
                $objexcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.$key,' '.$value['test'])
                    ->setCellValue('B'.$key,$value['test']);
            }

            // 设置活动单指数到第一个表,所以Excel打开这是第一个表
            $objexcel->setActiveSheetIndex(0);

            // 将输出重定向到一个客户端web浏览器(Excel2007)
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename='.$fileName);
            header('Cache-Control: max-age=0');

            //要是输出为Excel2007,使用 Excel2007对应的类,生成的文件名为.xlsx.如果是Excel2005,使用Excel5,对应生成.xls文件
            $io=new \PHPExcel_IOFactory();
            $objWriter = $io::createWriter($objexcel, 'Excel2007');
            //$objWriter = $io::createWriter($objexcel, 'Excel5');

            //支持浏览器下载生成的文档
            $objWriter->save('php://output');

        }
  }

杨佳乐 发布于  2024-4-30 11:08