• 每天进步一点点!

文章分类

推荐网站

常用手册

PHP输出UTF8编码和GB2312编码【原创】

//纯文本的utf8编码
header("Content-Type: text/plain;charset=utf8");

//html的utf8编码
header("Content-Type: text/html;charset=utf8");

//纯文本的gb2312编码
header("Content-Type: text/plain;charset=gb2312");

//html的gb2312编码
header("Content-Type: text/html;charset=gb2312");

 

类别:PHP | 浏览(72) | 评论(0) | 阅读全文>>

根据IP地址的获取地理位置【原创】

时常会遇到根据用户的IP获取用户所在的城市,如“欢迎来自北京的张三”。所以有了下面的程序。

<?php
/*
 * 获取用户的IP地址
 */
header("Content-Type: text/plain;charset=gb2312");

function get_online_ip($format = 0) {
	
	if (getenv ( 'HTTP_CLIENT_IP' ) && strcasecmp ( getenv ( 'HTTP_CLIENT_IP' ), 'unknown' )) {
		$onlineip = getenv ( 'HTTP_CLIENT_IP' );
	} elseif (getenv ( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp ( getenv ( 'HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
		$onlineip = getenv ( 'HTTP_X_FORWARDED_FOR' );
	} elseif (getenv ( 'REMOTE_ADDR' ) && strcasecmp ( getenv ( 'REMOTE_ADDR' ), 'unknown' )) {
		$onlineip = getenv ( 'REMOTE_ADDR' );
	} elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
		$onlineip = $_SERVER ['REMOTE_ADDR'];
	}
	preg_match ( "/[\d\.]{7,15}/", $onlineip, $onlineipmatches );
	$onlineip = $onlineipmatches [0] ? $onlineipmatches [0] : 'unknown';

	if ($format) {
		$ips = explode ( '.', $onlineip );
		for($i = 0; $i < 3; $i ++) {
			$ips [$i] = intval ( $ips [$i] );
		}
		return sprintf ( 'ddd', $ips [0], $ips [1], $ips [2] );
	} else {
		return $onlineip;
	}
}

/**
*用php的curl函数从网路上获取详细IP对应的地理未知
*/


function lazdf($ip){
	$url = "http://www.ip138.com/ips138.asp?ip=".$ip;
	$ipdz = http_request($url);
	preg_match("/<ul class=\"ul1\"><li>(.*?)<\/li>/i",$ipdz['body'],$jgarray);
	preg_match('/本站主数据:(.*)/', $jgarray[1], $place);
	echo   "欢迎来自".$place[1]."的朋友";

}

/**
* CURL封装
*/
function http_request($url,$post_data = array(),$header = array()) {
    $post_data = http_build_query($post_data);
    $header = $header ? $header : array ("Content-type: application/x-www-form-urlencoded; charset=gb2312");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    $response = curl_exec($ch);
   
    if(curl_errno($ch))
    {
        $response = "ERROR:".curl_error($ch);
    }
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return array('body'=>$response,'status'=>$status) ;
} 


lazdf(get_online_ip());

 

类别:PHP | 浏览(62) | 评论(0) | 阅读全文>>

PHP截取字符串函数【原创】

 
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8', $ext = '') {
	if ($code == 'UTF-8') {
		$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
		preg_match_all ( $pa, $string, $t_string );
		
		if (count ( $t_string [0] ) - $start > $sublen)
			return join ( '', array_slice ( $t_string [0], $start, $sublen ) ) . $ext;
		return join ( '', array_slice ( $t_string [0], $start, $sublen ) );
	} else {
		$start = $start * 2;
		$sublen = $sublen * 2;
		$strlen = strlen ( $string );
		$tmpstr = '';
		
		for($i = 0; $i < $strlen; $i ++) {
			if ($i >= $start && $i < ($start + $sublen)) {
				if (ord ( substr ( $string, $i, 1 ) ) > 129) {
					$tmpstr .= substr ( $string, $i, 2 );
				} else {
					$tmpstr .= substr ( $string, $i, 1 );
				}
			}
			if (ord ( substr ( $string, $i, 1 ) ) > 129)
				$i ++;
		}
		
		if (strlen ( $tmpstr ) < $strlen)
			$tmpstr .= $ext;
		return $tmpstr;
	}
}
类别:PHP | 浏览(76) | 评论(0) | 阅读全文>>

秒的整数值换算成时分秒形式【原创】

给定一个整数值的秒值,将其换算成时分秒形式的值,如:3599秒换算成:00:59:59

<?php

$second = 3599;

$h = floor($second / 3600) ;
$h = $h > 9 ? $h : '0' . $h;
$m = floor(($second % 3600) / 60) ;
$m = $m > 9 ? $m : '0' . $m;
$s = $second -  $h * 3600 - $m * 60;
$s = $s > 9 ? $s : '0' . $s;
$time =  $h . ':' . $m . ':' . $s;
echo $time;
类别:PHP | 浏览(83) | 评论(0) | 阅读全文>>

PHP error_log的使用【原创】

$array = array(1=>'one', 2=>'two', 3=>'three', 4=>4, 5=>5);
error_log('$array = ' . print_r($array, true) . "\n", 3, "/tmp/error.log");
//或
error_log('$array = ' . var_export($array, true) . "\n", 3, "/tmp/error.log");
tail -f /tmp/error.log 

输出:

$array = Array

(

    [1] => one

    [2] => two

    [3] => three

    [4] => 4

    [5] => 5

)

$array = array (

  1 => 'one',

  2 => 'two',

  3 => 'three',

  4 => 4,

  5 => 5,

)

注:

  1. error.log文件的路径和文件名可以随意指定,但必须有web服务器用户的可写权限
  2. var_export比print_r更能反映数据的类型
  3. 此函数在进行AJAX调试时很有用
  4. 在进行线上正式环境操作应多使用该函数,避免使用var_dump,print_r直接将数据打印到页面上。

 

类别:PHP | 浏览(106) | 评论(1) | 阅读全文>>