//纯文本的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");
时常会遇到根据用户的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());
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;
}
}
给定一个整数值的秒值,将其换算成时分秒形式的值,如: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;
$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,
)
注: