本篇内容主要讲解“php中常用的函数总结”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php中常用的函数总结”吧!
1、字符串编码转换
function str_iconv($str, $in_charset = 'UTF-8', $out_charset = 'GBK')
{
$str = mb_convert_encoding($str, $out_charset, $in_charset);
return $str;
}
2、数组编码转换
function arr_iconv($arr, $in_charset = 'UTF-8', $out_charset = 'GBK'){
$arr = eval('return ' . mb_convert_encoding(var_export($arr,true), $out_charset, $in_charset) . ';');
return $arr;
}
3、从内容中匹配出图片信息
function get_img_list_from_content($content, $b_only_img_url = true){
preg_match_all('/<img[^>]*?(?P<img_src_arr>src\s*=\s*([\'"]|"|'|')(?P<img_url_arr>.*?)([\'"]|"|'|'))[^>]*?>/msi', $content, $match);
$arr_temp = array();
if($match['img_url_arr'])
{
foreach($match['img_url_arr'] as $key => $img_url)
{
if($b_only_img_url){
$img_info = $img_url;
} else {
$img_info = array(
'img_tag' => $match[0][$key],
'img_src' => $match['img_src_arr'][$key],
'img_url' => $match['img_url_arr'][$key],
);
}
$arr_temp[] = $img_info;
}
}
return $arr_temp;
}
4、获取一个Hash编码
function make_hash_code($str)
{
if(empty($str))
return '';
$mdv = md5($str);
$mdv1 = substr($mdv,0,16);
$mdv2 = substr($mdv,16,16);
$crc1 = abs(crc32($mdv1));
$crc2 = abs(crc32($mdv2));
return bcmul($crc1,$crc2);
}
5、根据某天返回特定日期
function get_weekend_by_date(){
$year = date("Y");
$month = date("m");
$day = date("d");
$nowday = mktime(0,0,0,$month,$day,$year);
$w=(int)date("w",$nowday);
if($w==0){
$Sat = mktime(0,0,0,$month,$day - 1,$year);
$Sun = mktime(0,0,0,$month,$day + 1,$year);
} else {
$t = 6 - $w;
$Sat = mktime(0,0,0,$month,$day + $t,$year);
$Sun = mktime(0,0,0,$month,$day + $t + 2,$year);
}
return array("Sat"=>$Sat,"Sun"=>$Sun);
}
function get_timestamp_by_time_type($time_type = "today", $search_end_time = "")
{ // 支持的日期格式名称
// $time_type_arr = array('today', 'weekend', 'next_week', 'future_arr', 'history', 'time_to_time');
switch ($time_type)
{
case "today": //今天
$today = strtotime(date('Y-m-d'));
$tomorrow = $today+86400;
$querys["start_time"] = $tomorrow;
$querys["end_time"] = $today;
break;
case "weekend": //周末
$arr = mforum_get_weekend_by_date();
$querys["start_time"] = $arr["Sun"];
$querys["end_time"] = $arr["Sat"];
break;
case "next_week": //未来7天
$today = strtotime(date('Y-m-d'));
$next_week = $today+(86400*7);
$tomorrow = $today+86400;
$querys["start_time"] = $next_week;
$querys["end_time"] = $tomorrow;
break;
case "future_all": //未来全部
$nowtime=time();
$querys["end_time"] = $nowtime;
break;
case "history": //历史活动
$nowtime=time();
$querys["end_time"] = "< {$nowtime}";
break;
case "time_to_time": //选择时间段
$end_time = strtotime($search_end_time);
if(!empty($end_time)) {
$day = strtotime(date('Y-m-d',$end_time));
$tomorrow = $day+86400;
$querys["start_time"] = $tomorrow;
$querys["end_time"] = $day;
}
break;
default:
break;
}
return $querys;
}
6、根据过期时间判断剩余的天数
function check_remaining_days($expire_time)
{
// 获取当前时间
$cur_time = time();
$expire_time = (int)$expire_time;
$diff_time = ($expire_time - $cur_time);
$remaining_days_count = 0;
if($diff_time > 0) {
// 计算剩余的天数
$remaining_days_count = ceil($diff_time / (24 * 3600));
}
return $remaining_days_count;
}
7、获取某月的第一天和最后一天
// php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法
1.获取上个月第一天及最后一天.
echo date('Y-m-01', strtotime('-1 month'));
echo "<br/>";
echo date('Y-m-t', strtotime('-1 month'));
echo "<br/>";
2.获取当月第一天及最后一天.
$BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));
echo $BeginDate;
echo "<br/>";
echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
echo "<br/>";
3.获取当天年份、月份、日及天数.
echo " 本月共有:".date("t")."天";
echo " 当前年份".date('Y');
echo " 当前月份".date('m');
echo " 当前几号".date('d');
echo "<br/>";
4.使用函数及数组来获取当月第一天及最后一天,比较实用
function getthemonth($date)
{
$firstday = date('Y-m-01', strtotime($date));
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day")); return array($firstday,$lastday);
}
$today = date("Y-m-d");
$day=getthemonth($today);
echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1];
echo "<br/>";
5.封装了一个方法,开箱即用:
$year = 2017;
$month = 2;
function get_month_first_and_last_day($year, $month)
{
if(empty($year) || empty($month)) {
return array();
}
$date = $year . "-" . $month;
$begin_date = date('Y-m-01 00:00:00', strtotime($date));
$last_date = date('Y-m-d 23:59:59', strtotime("$begin_date +1 month -1 day"));
return array('begin_date' => $begin_date, 'last_date' => $last_date);
}
$ret = get_month_first_and_last_day($year, $month);
print_r($ret);Array(
[begin_date] => 2017-02-01 00:00:00
[last_date] => 2017-02-28 23:59:59)
8、根据二维数组的数据字段名返回其对应的值数组
* 根据二维数组的数据字段名返回其对应的值数组
*
* @param array $rows 二维数组
* @param string $field_name 字段名
* @param boolean $b_off_empty 是否排除空值,默认:true
* @return array
*/
function array_values_by_field_name($rows, $field_name, $b_off_empty = false){
$ret = array();
foreach($rows as $row) {
if(isset($row[$field_name])) {
if($b_off_empty) {
if(!empty($row[$field_name])) {
$ret[] = $row[$field_name];
}
} else {
$ret[] = $row[$field_name];
}
}
}
return $ret;
}
到此,相信大家对“php中常用的函数总结”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!