字符串操作

strpos函数返回boolean值.FALSETRUE不用多说.用 “===”进行判断.strpos在执行速度上都比以上两个函数快,另外strpos有一个参数指定判断的位置,但是默认为空.意思是判断整个字符串.缺点是对中文的支持不好.

字符串查找
if(strpos('www.textfind.com','textfind') !== false){ 
 echo '包含textfind';
}else{
 echo '不包含textfind';
}

字符串替换
echo str_replace("world","Shanghai","Hello world!");  //"world" 替换为 "Shanghai":

获取访客用户IP地址

function getip() {
  static $ip = '';
  $ip = $_SERVER['REMOTE_ADDR'];
  if(isset($_SERVER['HTTP_CDN_SRC_IP'])) {
    $ip = $_SERVER['HTTP_CDN_SRC_IP'];
  } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
  } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
    foreach ($matches[0] AS $xip) {
      if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
        $ip = $xip;
        break;
      }
    }
  }
  return $ip;
}

穷举列出两个日期间的所有日期

/**
 * 获取两个时间之间的日期
 * @param $startDate
 * @param $endDate
 * @return array
 */
function getDatesBetweenTwoDays($startDate, $endDate)
{
    $dates = array();
    if (strtotime($startDate) > strtotime($endDate)) {
        // 如果开始日期大于结束日期,直接return 防止下面的循环出现死循环
        return $dates;
    } elseif ($startDate == $endDate) {
        // 开始日期与结束日期是同一天时
        array_push($dates, $startDate);
        return $dates;
    } else {
        array_push($dates, $startDate);
        $currentDate = $startDate;
        do {
            $nextDate = date('Y-m-d', strtotime($currentDate . ' +1 days'));
            array_push($dates, $nextDate);
            $currentDate = $nextDate;
        } while ($endDate != $currentDate);
        return $dates;
    }
}
//返回日期的字符串格式
public function prDates($start,$end){//获取两个日期之间的所有日期 2016-04-02至2016-04-16  
    $dt_start = strtotime($start);
    $dt_end = strtotime($end);
    while ($dt_start<=$dt_end){
        $value.= date('Y-m-d',$dt_start).",";
        $dt_start = strtotime('+1 day',$dt_start);
    }
    return $value;
}
$datestr=$this->prDates($j_Date1,$j_Date2);
$d=substr($datestr,0,-1);
$array=explode(',',$d);
print_r($array);

写日志文件(追加写入txt)

function write_log($file,$log){
        file_put_contents($file,"\n ".date('Y-m-d H:i:s',time())." | ".getip()." |   http://".$_SERVER['SERVER_NAME'].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]."\n |-->".$log,FILE_APPEND);
}

强制下载

//PHP强制下载文件有时我们不想让浏览器直接打开文件,如PDF文件,而是要直接下载文件,那么以下函数可以强制下载文件,
//函数中使用了application/octet-stream头类型。
function download($filename){
     if ((isset($filename))&&(file_exists($filename))){ 
            header("Content-length: ".filesize($filename));
                    header('Content-Type: application/octet-stream'); 
                           header('Content-Disposition: attachment; filename="' . $filename . '"');
                                  readfile("$filename");     } else {
                                          echo "Looks like file does not exist!";
                                          } }
 // 使用方法如下:download('/down/testfile.zip');
最后编辑: admin  文档更新时间: 2021-03-16 21:50   作者:admin