$time = gettimeofday(void);
//文件初始化 start
if(@filesize("time.text")<=0){
$fd_time = fopen("time.text","w+");
fputs($fd_time,$time[sec]);
fclose($fd_time);
$fd_time = fopen("ip.text","w+");
fclose($fd_time);
}
//文件初始化 over
//更新时间 start
$tamp = file("time.text");
$equal = ($time[sec] - $tamp[0]);
if($equal > 60){
$fd_time = fopen("time.text","w+");
fputs($fd_time,"");
fclose($fd_time);
}
//更新时间 over
//检查ip start
$fd_ip = fopen("ip.text","a+");
//获取用户ip地址
if ($http_server_vars["http_x_forwarded_for"])
{
$ip = $http_server_vars["http_x_forwarded_for"];
}
elseif ($http_server_vars["http_client_ip"])
{
$ip = $http_server_vars["http_client_ip"];
}
elseif ($http_server_vars["remote_addr"])
{
$ip = $http_server_vars["remote_addr"];
}
elseif (getenv("http_x_forwarded_for"))
{
$ip = getenv("http_x_forwarded_for");
}
elseif (getenv("http_client_ip"))
{
$ip = getenv("http_client_ip");
}
elseif (getenv("remote_addr"))
{
$ip = getenv("remote_addr");
}
else
{
$ip = "unknown";
}
$ip_adds = file("ip.text");
for($i;$i<count($ip_adds);$i++){
if($ip." "==$ip_adds[$i]){
$ip_check = 1;
break;
}
}
if($ip_check!=1){
fputs($fd_ip,$ip." ");
}
fclose($fd_ip);
//检查ip over
$ip_adds = count(file("ip.text"));
?>
调用 方法
<?php
/*
* 标题:简单在线人数统计
*
* 作者:邓东东
*
* 创建于:2010-4-27下午09:42:31
*
* 技术支持:php100学习交流22 :108610071
**/
?>
<?
include("online.php");
$fd_ip = file("ip.text");
?>
<title>在线人数</title>
<center><font color=blue size=6>php100论坛在线总人数为:<font color=red size=8><?=$ip_adds?>08610071</font>人<br>
用户ip:<?
echo "<select>";
for($i = 0;$i < count($ip_adds);$i++){
echo"<option value = '$i'>".substr($fd_ip[$i],0,0-strlen(strrchr($fd_ip[$i],'.'))).".*</option> ";
}
echo "</select>";
?></font></center>
下面写了二个php 字符串截取函数,方法很简单,string要截取的字符串,sublen长度,$start开位置哦。代码如下 | 复制代码 |
function my_sub_str($string, $sublen, $start) if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."..."; |
/**
* 字符串截取 不加"..."
*/
代码如下 | 复制代码 |
function my_sub_str_00($string, $sublen, $start) return join('', array_slice($t_string[0], $start, $sublen)); |
//字符截取测试
代码如下 | 复制代码 |
$string ="www.111cn.net中国web第一站"; echo my_sub_str($string, 10, 0); //输出 www.111cn.... |
if(function_exists('curl_init'))
{
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url_with_get);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $post);
curl_setopt($ch, curlopt_returntransfer, true);
$result = curl_exec($ch);
curl_close($ch);
}
else
{
$content = http_build_query($post)
$content_length = strlen($content);
$context =
array('http' =>
array('method' => 'post',
'user_agent' => $user_agent,
'header' => 'content-type: ' . $content_type . " " .
'content-length: ' . $content_length,
'content' => $content));
$context_id = stream_context_create($context);
$sock = fopen($url_with_get, 'r', false, $context_id);
$result = '';
if ($sock)
{
while (!feof($sock))
$result .= fgets($sock, 4096);
fclose($sock);
}
return $result;
}
}
测试代码
代码如下 | 复制代码 |
|
代码如下 | 复制代码 |
class validation{ function check_email($value){ //check a email address return (bool) preg_match('/^[a-z0-9._%+-]+@(?:[a-z0-9-]+.)+[a-z]{2,4}$/i', $value); } function check_ip($value){ //check a ip address return (bool) preg_match("/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){4}$/", $value."."); } function check_date($date, $format="dd/mm/yy"){ //check a date if(!preg_match("/([0-9]+)([./-])([0-9]+)(\2)([0-9]+)/", $date, $m)) return false; $f = explode("/", $format); $d[$f[0]] = $m[1]; $d[$f[1]] = $m[3]; $d[$f[2]] = $m[5]; return checkdate($d['mm'], $d['dd'], $d['yyyy'].$d['yy']); } function check_url($url){ //check a url return (bool) preg_match("/^(?:(?:ht|f)tp(?:s?)://|~/|/)?(?:(?:w+.)+)w+(?::d+)?(?:(?:/[^/?#]+)+)?/?(?:?[^?]*)?(#.*)?$/i", $url); } } |
//调用方法
代码如下 | 复制代码 |
$validation = new validation(); //检测邮箱 var_dump($validation->check_email("abc@abc.abc.com"));//true var_dump($validation->check_email("abc.abc@abc.com"));//false |
//ip检测
代码如下 | 复制代码 |
var_dump($validation->check_ip("127.0.0.1.0")); var_dump($validation->check_ip("256.0.0.1")); |
//日期格式检测
代码如下 | 复制代码 |
//true |
/*
此类的使用方法非常简单吧,只要创建类就可以轻松的验证邮箱,日期,地址,ip是否为合法的的哦。
*/
php正则判断中文字符
php教程正则判断中文字符
$str = "中国";
echo $str;
echo "<hr>";//if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) { //只能在gb2312情况下使用
if (preg_match("/^[x7f-xff]+$/", $str)) { //兼容gb2312,utf-8
echo "正确输入";
} else {
echo "错误输入";
}
?>
双字节字符编码范围
1. gbk (gb2312/gb18030)
x00-xff gbk双字节编码范围
x20-x7f ascii
xa1-xff 中文 gb2312
x80-xff 中文 gbk
2. utf-8 (unicode)
u4e00-u9fa5 (中文)
x3130-x318f (韩文
xac00-xd7a3 (韩文)
u0800-u4e00 (日文)