php 天气预报代码 采集自中央气象台范围覆盖全国

 更新时间:2016年11月25日 16:27  点击:1794
天气预报信息采集自中央气象台,信息准确,覆盖面广,代码简单。返回是json代码,可以用于客户端调用,也可以在服务器端处理后显示,笔者的wordpress天气预报插件就是使用的这段代码哦。需要注意的是,这段代码会在服务器产生缓存文件,需要在当前目录中新建data文件夹,保证文件夹可写

    

 代码如下 复制代码

作者:    freemouse
     主页:    www.cnphp.info
     接口demo: http://www.cnphp.info/tianqi
     用法: 可以直接调用上面的地址会显示当地的天气信息,也可以这样用
    http://www.cnphp.info/tianqi/?q=江苏省南京市
    **/

    $p = $_get['q'];
    $k = 0;
    $encoding = mb_detect_encoding($p);
    if($encoding != "utf-8"){
        $p = mb_convert_encoding($p,"utf-8","gbk");
    }
    $p_arr = array(
       "01" => "北京",
      "02" => "上海",
      "03" => "天津",
      "04" => "重庆",
      "05" => "黑龙江",
      "06" => "吉林",
      "07" => "辽宁",
      "08" => "内蒙古",
      "09" => "河北",
      "10" => "山西",
      "11" => "陕西",
      "12" => "山东",
      "13" => "新疆",
      "14" => "西藏",
      "15" => "青海",
      "16" => "甘肃",
      "17" => "宁夏",
      "18" => "河南",
      "19" => "江苏",
      "20" => "湖北",
      "21" => "浙江",
      "22" => "安徽",
      "23" => "福建",
      "24" => "江西",
      "25" => "湖南",
      "26" => "贵州",
      "27" => "四川",
      "28" => "广东",
      "29" => "云南",
      "30" => "广西",
      "31" => "海南",
      "32" => "香港",
      "33" => "澳门",
      "34" => "台湾"
    );
 
    function find(&$item,$key,$data){
        global $k;
        if(preg_match("/$item/u",$data)){
           $k = $key;
        }
    }
 
    function get_data_arr($key){
        if(!file_exists("./data/city{$key}.xml")){
            $c =file_get_contents( "http://m.weather.com.cn/data5/city{$key}.xml" );
            file_put_contents("./data/city{$key}.xml",$c);
        }
        else{
            $c = file_get_contents("./data/city{$key}.xml");
        }
       $arr = explode(",",$c);
       foreach($arr as $v){
           $data = explode("|",$v);
           $ret[$data[0]] = $data[1];
       }
       return $ret;
    }
 
    array_walk($p_arr,'find',$p);
    array_walk(get_data_arr($k),'find',$p);
    array_walk(get_data_arr($k),'find',$p);
    $ccode = get_data_arr($k);
    echo file_get_contents("http://m.weather.com.cn/data/{$ccode[$k]}.html");

/

 代码如下 复制代码

*  一、本程序基于div+css教程 新型架构php探针,免费开源的自由软件,功能强大,结构清晰,使用方便。
  1.支持windows,linux,unix,freebsd,sun solar系统
  2.支持ie6,ie7,firefox,google chrome等浏览器。
  
  二、主要用途及适用对象:
  1.熟悉php编程的业余爱好者及专业开发人员。
  2.机房管理人员配置linux(windows)+php+mysql教程+zend系统环境,检测系统是否配置成功。
  3.对于购买虚拟主机的用户,用于测试服务器性能。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*/
 //////////////////以下两变量可以修改配制
 //如果这个探针你是用来验示的,你可以在下面变量中输入相关信息(会显示在页面底部
    header("content-type: text/html; charset=utf-8");
    error_reporting(e_error | e_warning | e_parse);
 ob_start();
    
    $valint = (false == empty($_post['pint']))?$_post['pint']:"未测试";
    $valfloat = (false == empty($_post['pfloat']))?$_post['pfloat']:"未测试";
    $valio = (false == empty($_post['pio']))?$_post['pio']:"未测试";
    $mysqlreshow = "none";
    $mailreshow = "none";
    $funreshow = "none";
    $opreshow = "none";
    $sysreshow = "none";
//============   定义常量 用于替换模板输出变量  =======================
  //define("yes", "<span class='resyes'>yes</span>");
  //define("no", "<span class='resno'>no</span>");
  define("yes", "<span class='resyes'>√</span>");
  define("no", "<span class='resno'>×</span>");

//=================================================================
    define("icon", "<span class='icon'>2</span>&nbsp;");
    $phps教程elf = $_server[php_self] ? $_server[php_self] : $_server[script_name];
    define("phpself", preg_replace("/(.{0,}?/+)/", "", $phpself));
    
    if ($_get['act'] == "phpinfo")
    {
        phpinfo();
        exit();
    }
    elseif($_post['act'] == "整型测试")
    {
        $valint = test_int();
    }
    elseif($_post['act'] == "浮点测试")
    {
        $valfloat = test_float();
    }
    elseif($_post['act'] == "io测试")
    {
        $valio = test_io();
    }
    elseif($_post['act'] == "connect")
    {
        $mysqlreshow = "show";
        $mysqlre = "mysql连接测试结果:";
        $mysqlre .= (false !== @mysql_connect($_post['mysqlhost'], $_post['mysqluser'], $_post['mysqlpassword']))?"mysql服务器连接正常, ":"mysql服务器连接失败, ";
        $mysqlre .= "数据库教程 <b>".$_post['mysqldb']."</b> ";
        $mysqlre .= (false != @mysql_select_db($_post['mysqldb']))?"连接正常":"连接失败";
    }
    elseif($_post['act'] == "sendmail")
    {
        $mailreshow = "show";
        $mailre = "mail邮件发送测试结果:发送";
        $mailre .= (false !== @mail($_post["mailreceiver"], "uenuprobe mail server test.", "this email is sent by uenuprobe. copyright uenucom http://www.uenu.com"))?"完成":"失败";
    }
    elseif($_post['act'] == "function_check")
    {
        $funreshow = "show";
        $funre = "函数 <b>".$_post['funname']."</b> 支持状况检测结果:".isfun($_post['funname']);
    }
    elseif($_post['act'] == "configuration_check")
    {
        $opreshow = "show";
        $opre = "配置参数 <b>".$_post['opname']."</b> 检测结果:".getcon($_post['opname']);
    }
 
//========================================================================

//========================================================================
   switch (php_os)
    {
        case "linux":
        $sysreshow = (false != ($sysinfo = sys_linux()))?"show":"none";
        break;
        case "freebsd":
        $sysreshow = (false != ($sysinfo = sys_freebsd()))?"show":"none";
        break;
  case "windows":
        //$sysreshow = (false != ($sysinfo = sys_windows()))?"show":"none";
  $sysinfo['uptime'] ="对不起windows系统不支持";
        break;
        default:
        break;
    }
    
//========================================================================

?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="common/control.css">
<script type="text/网页特效" language="javascript" src="common/admin.otable.js"></script>
<script type="text/javascript" language="javascript" src="common/include.func.js"></script>
<title>服务器信息</title>
<style type="text/css">
.resyes {
 font-size: 12px;
 color: #090;
 font-weight:bold;
 font-family: verdana;
}
.resno {
 font-size: 12px;
 color: #f00;
 font-weight:bold;
 font-family: verdana;
}
.check00{
 color: #630; 
}
</style>
<script type="text/javascript">
window.onload=function(){
 var otable=document.getelementbyid("otable");
 for(var i=0;i<otable.rows.length;i++){
  if(i%2==0) //偶数行时
   otable.rows[i].classname="altrow";
 }
 
 var otable2=document.getelementbyid("otable2");
 for(var i=0;i<otable2.rows.length;i++){
  if(i%2==0) //偶数行时
   otable2.rows[i].classname="altrow";
 }
 
 var otable3=document.getelementbyid("otable3");
 for(var i=0;i<otable3.rows.length;i++){
  if(i%2==0) //偶数行时
   otable3.rows[i].classname="altrow";
 }
 
 var otable4=document.getelementbyid("otable4");
 for(var i=0;i<otable4.rows.length;i++){
  if(i%2==0) //偶数行时
   otable4.rows[i].classname="altrow";
 }
 
 var otable5=document.getelementbyid("otable5");
 for(var i=0;i<otable5.rows.length;i++){
  if(i%2==0) //偶数行时
   otable5.rows[i].classname="altrow";
 }
}
</script>
</head>

<body class="contentbody">
<div class="maindiv">
<span class="ctitle">服务器信息</span>
<ul class="cmenu">
 <li><a href="server_info.php?action=">服务器特征</a></li>
 <li><a href="server_info.php?action=phpinfo">php基本特征</a></li>
 <li><a href="server_info.php?action=otherinfo">php组件支持状况</a></li>
 <li><a href="server_info.php?action=dbinfo">数据库支持状况</a></li>
 <li><a href="server_info.php?action=testinfo">性能检测</a></li>
    <li><a href="server_moreinfo.php">服务器详细信息</a></li>
</ul>

<div class="concontent">
<?php
switch($_get['action']){
 case '':
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="ctable" id="otable" align="center">
<tr>
 <th colspan="2" align="center">服务器特征</th>
</tr>
<tr>
 <td width="150">服务器时间</td>
    <td><?php echo gmdate("y年m月d日 h:i:s",time());?>&nbsp;(格林威治标准时间)&nbsp;&nbsp;<?php echo gmdate("y年n月j日 h:i:s",time()+8*3600)?>&nbsp;(北京时间)</td>
</tr>
<tr>
    <td>服务器域名</td>
    <td><?php echo("<a href="http://$_server[server_name]"  title=访问此域名 target=_blank>$_server[server_name]</a>"); ?></td>
</tr>
<tr>
 <td>服务器ip地址</td>
    <td><?php $host_ip=gethostbyname($_server["server_name"]); echo($host_ip);?></td>
</tr>
<tr>
 <td>服务器操作系统</td>
    <td><?php $os = explode(" ", php_uname()); echo $os[0]; echo "&nbsp;&nbsp;";
 if ($os[0] =="windows") {echo "主机名称:".$os[2];} else {echo "内核版本:".$os[2];}?></td>
</tr>
<tr>
 <td>服务器运行时间</td>
    <td><?php if ($sysinfo['uptime']!=""){ echo $sysinfo['uptime'];} else  echo "对不起windows系统不支持"; ?></td>
</tr>
<tr>
 <td>服务器操作系统文字编码</td>
    <td><?php echo($_server["http_accept_language"]); ?></td>
</tr>
<tr>
 <td>服务器解译引擎</td>
    <td><?php echo($_server["server_software"]); ?></td>
</tr>
<tr>
 <td>web服务端口</td>
    <td><?php echo($_server["server_port"]); ?></td>
</tr>
<tr>
 <td>服务器管理员</td>
    <td><a href="mailto:<?php echo $_server['server_admin'];?>"><?php echo $_server['server_admin'];?></a></td>
</tr>
<tr>
 <td>本文件路径</td>
    <td><?php echo($_server["script_filename"]);?></td>
</tr>
<tr>
 <td>服务端剩余空间</td>
    <td><?php echo intval(diskfreespace(".") / (1024 * 1024)).'mb';?></td>
</tr>
<tr>
 <td>系统当前用户名</td>
    <td><?php echo @get_current_user();?></td>
</tr>
</table>
<br />
<!-- 仅在windows 环境中输出-->
<?php if(("show" !==$sysreshow) & ("0"!= $_env["number_of_processors"])& (""!= $_env["number_of_processors"])){?>
<div class="info3">服务器处理器</div><div class="info4">cpu个数:<?php echo $_env["number_of_processors"]?> <?php echo "&nbsp;&nbsp;".$_env["processor_identifier"]; echo "&nbsp;&nbsp;运行级别:".$_env["processor_level"]; echo "&nbsp;&nbsp;版本:".$_env["processor_revision"];?></div>
<?php }?>
<!-- 仅在windows 环境中输出结束-->
<!-- linux or unix 参数输出-->
<?php if(("show"==$sysreshow)&("0" != $sysinfo['cpu']['num'])&("" != $sysinfo['cpu']['num'])){?>
<div class="info3">服务器处理器</div><div class="info4">cpu个数:<?php echo $sysinfo['cpu']['num']?> &nbsp;&nbsp;<?php echo $sysinfo['cpu']['detail']?></div>
<?php }?>
<?php if("show"==$sysreshow){?>
<div class="info3">内存使用状况</div><div class="info4">
<?php echo $sysinfo['memtotal']?>m, 已使用
<?php echo $sysinfo['memused']?>m, 空闲
<?php echo $sysinfo['memfree']?>m, 使用率
<?php echo $sysinfo['mempercent']?>%</div>
<div class="info3">swap区</div><div class="info4">
共<?php echo $sysinfo['swaptotal']?>m, 已使用
<?php echo $sysinfo['swapused']?>m, 空闲
<?php echo $sysinfo['swapfree']?>m, 使用率
<?php echo $sysinfo['swappercent']?>%</div>
<div class="info3">系统平均负载</div><div class="info4"><?php echo $sysinfo['loadavg']?></div>
<?php }?>
<!-- linux or unix 参数输出结束-->
<?php
  break;
 case 'phpinfo':
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="ctable" id="otable" align="center">
<tr>
 <th colspan="4" align="center">php基本特征</th>
</tr>
<tr>
 <td width="30%">php版本</td>
    <td width="20%"><?php echo php_version;?></td>
 <td width="30%">php运行方式</td>
    <td><?php /**strtoupper(php_sapi_name());**/ echo ucwords(php_sapi_name());?></td>
</tr>
<tr>
 <td>支持zend编译运行&nbsp;&nbsp;(<?php if($zend="yes") {echo "版本:";echo zend_version();}?>)</td>
    <td><?php echo $zend=(get_cfg_var("zend_optimizer.optimization_level")||get_cfg_var("zend_extension_manager.optimizer_ts")||get_cfg_var("zend_extension_ts")) ?yes:no?></td>
 <td>运行于安全模式</td>
    <td><?php if(get_cfg_var("safemode")){echo("是");}else echo("否"); ?></td>
</tr>
<tr>
 <td>自动定义全局变量&nbsp;register_globals</td>
    <td><?php echo @get_cfg_var("register_globals")?'on' : 'off';?></td>
 <td>允许使用url打开文件allow_url_fopen</td>
    <td><?php echo get_cfg_var("allow_url_fopen")=="1"?yes:no?></td>
</tr>
<tr>
 <td>允许动态加载链接库enable_dl</td>
    <td><?php echo get_cfg_var("enable_dl")=="1"?yes:no?></td>
 <td>显示错误信息&nbsp;display_errors</td>
    <td><?php echo get_cfg_var("display_errors")=="1"?yes:no?></td>
</tr>
<tr>
 <td>短标记&lt;? ?&gt;支持</td>
    <td><?php echo @get_cfg_var("short_open_tag")?yes:no;?></td>
 <td>标记&lt;% %&gt;支持</td>
    <td><?php echo @get_cfg_var("asp教程_tags")?yes:no;?></td>
</tr>
<tr>
 <td>cookie支持</td>
    <td><?php echo isset($http_cookie_vars)?yes:no;?></td>
 <td>session支持</td>
    <td><?php echo function_exists(session_start)?yes:no;?></td>
</tr>
<tr>
 <td>浮点运算有效数字显示位数</td>
    <td><?php echo @get_cfg_var("precision");?></td>
 <td>强制y2k兼容</td>
    <td><?php echo @get_cfg_var("y2k_compliance")?yes:no;?></td>
</tr>
<tr>
 <td>被禁用的函数disable_functions</td>
    <td><?php $disused = @get_cfg_var("disable_functions")?"1":"0";
if($disused =="1")
{echo '<a href="#" title="
'.@get_cfg_var("disable_functions").'
">'."more".'</a>';}
else {echo "none";}?></td>
 <td>程序最长运行时间max_execution_time</td>
    <td><?php echo(get_cfg_var("max_execution_time")."秒");?></td>
</tr>
<tr>
 <td>程序最多允许使用内存量 memory_limit</td>
    <td><?php echo @get_cfg_var("memory_limit");?></td>
 <td>post最大字节数&nbsp;post_max_size</td>
    <td><?php echo @get_cfg_var("post_max_size");?></td>
</tr>
<tr>
 <td>允许最大上传文件&nbsp;upload_max_filesize</td>
    <td><?php echo @get_cfg_var("file_uploads")?@get_cfg_var("upload_max_filesize") : $error;?></td>
 <td>php信息 phpinfo</td>
    <td><?php echo (false!==eregi("phpinfo",$disfuns))?no:"<a href='$phpself?act=phpinfo' target='_blank' class='static'>phpinfo</a>"?></td>
</tr>
<tr>
 <td>html错误显示</td>
    <td><?php echo @get_cfg_var("html_errors")?yes:no;?></td>
 <td>调试器地址/端口</td>
    <td><?php echo $debugerhost=@get_cfg_var("debugger.host")?yes:no;if ($debugerhost =="yes") {echo @get_cfg_var("debugger.port")?yes:no;}?></td>
</tr>
<tr>
 <td>smtp支持</td>
    <td><?php echo @get_cfg_var("smtp")?yes:no;?></td>
 <td>smtp地址</td>
    <td><?php echo @get_cfg_var("smtp");?></td>
</tr>
</table>
<?php
  break;
 case 'otherinfo': 
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="ctable" id="otable" align="center">
<tr>
 <th colspan="4" align="center">php组件支持状况</th>
</tr>
<tr>
 <td width="30%">拼写检查 aspell library</td>
    <td width="20%"><?php echo function_exists(aspell_new)?yes:no;?></td>
    <td width="30%">高精度数学运算 bcmath</td>
    <td><?php echo function_exists(bcadd)?yes:no;?></td>
</tr>
<tr>
 <td>历法运算 calendar</td>
    <td><?php echo function_exists(jdtofrench)?yes:no;?></td>
 <td>图形处理 gd library</td>
    <td><?php echo function_exists(imageline)?yes:no;?></td>
</tr>
<tr>
 <td>类/对象支持</td>
    <td><?php echo function_exists(class_exists)?yes:no;?></td>
 <td>字串类型检测支持</td>
    <td><?php echo function_exists(ctype_upper)?yes:no;?></td>
</tr>
<tr>
 <td>iconv编码支持</td>
    <td><?php echo function_exists(iconv)?yes:no;?></td>
 <td>mcrypt加密处理支持</td>
    <td><?php echo function_exists(mcrypt_cbc)?yes:no;?></td>
</tr>
<tr>
 <td>哈稀计算 mhash</td>
    <td><?php echo function_exists(mhash)?yes:no;?></td>
 <td>openssl支持</td>
    <td><?php echo function_exists(openssl_open)?yes:no;?></td>
</tr>
<tr>
 <td>prel相容语法 pcre</td>
    <td><?php echo function_exists(preg_match)?yes:no;?></td>
 <td>正则扩展(兼容perl)支持</td>
    <td><?php echo function_exists(preg_match)?yes:no;?></td>
</tr>
<tr>
 <td>socket支持</td>
    <td><?php echo function_exists(fsockopen)?yes:no;?></td>
 <td>流媒体支持</td>
    <td><?php echo function_exists(stream_context_create)?yes:no;?></td>
</tr>
<tr>
 <td>tokenizer支持</td>
    <td><?php echo function_exists(token_name)?yes:no;?></td>
 <td>url支持</td>
    <td><?php echo function_exists(parse_url)?yes:no;?></td>
</tr>
<tr>
 <td>wddx支持(web distributed data exchange)</td>
    <td><?php echo function_exists(wddx_add_vars)?yes:no;?></td>
 <td>压缩文件支持(zlib)</td>
    <td><?php echo function_exists(gzclose)?yes:no;?></td>
</tr>
<tr>
 <td>xml解析</td>
    <td><?php echo function_exists(xml_set_object)?yes:no;?></td>
 <td>ftp</td>
    <td><?php echo function_exists(ftp_login)?yes:no;?></td>
</tr>
<tr>
 <td>目录存取协议(ldap)支持</td>
    <td><?php echo function_exists(ldap_close)?yes:no;?></td>
 <td>yellow page系统支持</td>
    <td><?php echo function_exists(yp_match)?yes:no;?></td>
</tr>
<tr>
 <td>php和java综合支持</td>
    <td><?php echo function_exists(java_last_exception_get)?yes:no;?></td>
 <td>imap电子邮件系统支持</td>
    <td><?php echo function_exists(imap_close)?yes:no;?></td>
</tr>
<tr>
 <td>snmp网络管理协议支持</td>
    <td><?php echo function_exists(snmpget)?yes:no;?></td>
 <td>vmailmgr邮件处理支持</td>
    <td><?php echo function_exists(vm_adduser)?yes:no;?></td>
</tr>
<tr>
 <td>pdf文档支持</td>
    <td><?php echo function_exists(pdf_close)?yes:no;?></td>
 <td>fdf表单资料格式支持</td>
    <td><?php echo function_exists(fdf_close)?yes:no;?></td>
</tr>
</table>
<?php
  break;
 case 'dbinfo': 
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="ctable" id="otable" align="center">
<tr>
 <th colspan="4" align="center">数据库支持状况</th>
</tr>
<tr>
 <td width="30%">mysql数据库支持</td>
    <td width="20%"><?php echo function_exists(mysql_close)?yes:no;?></td>
 <td width="30%">mysql数据库持续连接</td>
    <td><?php echo @get_cfg_var("mysql.allow_persistent")?yes:no;?></td>
</tr>
<tr>
 <td>mysql最大连接数</td>
    <td><?php echo @get_cfg_var("mysql.max_links")==-1 ? "不限" : @get_cfg_var("mysql.max_links");?></td>
 <td>odbc数据库连接</td>
    <td><?php echo function_exists(odbc_close)?yes:no;?></td>
</tr>
<tr>
 <td>sql server数据库支持</td>
    <td><?php echo function_exists(mssql_close)?yes:no;?></td>
 <td>msql数据库支持</td>
    <td><?php echo function_exists(msql_close)?yes:no;?></td>
</tr>
<tr>
 <td>postgre sql数据库支持</td>
    <td><?php echo function_exists(pg_close)?yes:no;?></td>
 <td>oracle数据库支持</td>
    <td><?php echo function_exists(ora_close)?yes:no;?></td>
</tr>
<tr>
 <td>oracle 8 数据库支持</td>
    <td><?php echo function_exists(ocilogoff)?yes:no;?></td>
 <td>dbase数据库支持</td>
    <td><?php echo function_exists(dbase_close)?yes:no;?></td>
</tr>
<tr>
 <td>sybase数据库支持</td>
    <td><?php echo function_exists(sybase_close)?yes:no;?></td>
 <td>dba数据库支持</td>
    <td><?php echo function_exists(dba_close)?yes:no;?></td>
</tr>
<tr>
 <td>dbm数据库支持</td>
    <td><?php echo function_exists(dbmclose)?yes:no;?></td>
 <td>dbx数据库支持</td>
    <td><?php echo function_exists(dbx_close)?yes:no;?></td>
</tr>
<tr>
 <td>db++数据库支持</td>
    <td><?php echo function_exists(dbplus_close)?yes:no;?></td>
 <td>frontbase数据库支持</td>
    <td><?php echo function_exists(fbsql_close)?yes:no;?></td>
</tr>
<tr>
 <td>filepro数据库支持</td>
    <td><?php echo function_exists(filepro)?yes:no;?></td>
 <td>informix数据库支持</td>
    <td><?php echo function_exists(ifx_close)?yes:no;?></td>
</tr>
<tr>
 <td>lotus notes数据库支持</td>
    <td><?php echo function_exists(notes_version)?yes:no;?></td>
 <td>interbase数据库支持</td>
    <td><?php echo function_exists(ibase_close)?yes:no;?></td>
</tr>
<tr>
 <td>ingres数据库支持</td>
    <td><?php echo function_exists(ingres_close)?yes:no;?></td>
 <td>hyperwave数据库支持</td>
    <td><?php echo function_exists(hw_close)?yes:no;?></td>
</tr>
<tr>
 <td>ovrimos sql数据库连接支持</td>
    <td><?php echo function_exists(ovrimos_close)?yes:no;?></td>
 <td>sesam数据库连接支持</td>
    <td><?php echo function_exists(sesam_disconnect)?yes:no;?></td>
</tr>
<tr>
 <td>sqlite数据库连接支持</td>
    <td><?php echo function_exists(sqlite_close)?yes:no;?></td>
 <td>adabas d数据库连接支持</td>
    <td><?php echo function_exists(ada_close)?yes:no;?></td>
</tr>
</table>
<?php
  break;
 case 'testinfo': 
?>
<form action="<?php echo phpself."?action=testinfo&icon=$icon&#testinfo1"?>" method="post">
<!--服务器性能检测-->
<div id="testinfo1">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="ctable" id="otable" align="center">
<tr><th colspan="4">服务器性能检测</th></tr>
<tr align="center">
 <td width="25%">参照对象</td>
 <td width="25%">整数运算能力检测(1+1运算300万次)</td>
 <td width="25%">浮点运算能力检测(开平方300万次)</td>
 <td width="25%">数据i/o能力检测(读取10k文件1万次)</td>
</tr>
<tr align="center">
 <td>红色主机单线型(双核)</td>
 <td>0.33 秒</td>
 <td>0.95 秒</td>
 <td>0.03 秒</td>
</tr>
<tr align="center">
 <td>红色主机双线型(四核)</td>
 <td>0.25 秒</td>
 <td>0.79 秒</td>
 <td>0.03 秒</td>
</tr>
<tr align="center">
 <td>xeon(tm) 2.80*2+4g+centos 5.2</td>
 <td>0.068秒</td>
 <td>0.086秒</td>
 <td>小于0.100秒</td>
</tr>
<tr align="center">
 <td>xeon(tm) 2.80ghz+2g+freebsd 4.0</td>
 <td>0.501秒</td>
 <td>0.694秒</td>
 <td>小于0.100秒</td>
</tr>
<tr align="center" height="50">
 <td>本台服务器</td>
 <td><span style="color:#390"><?php echo $valint;?></span> <input type="submit" value="整型测试" name="act" class="button" /></td>
 <td><span style="color:#06c"><?php echo $valfloat;?></span> <input type="submit" value="浮点测试" name="act" class="button" /></td>
 <td><span style="color:#f60"><?php echo $valio;?></span> <input type="submit" value="io测试" name="act" class="button" /></td>
</tr>
</table>
</div>
<input type="hidden" name="pint" value="<?php echo $valint;?>" />
<input type="hidden" name="pfloat" value="<?php echo $valfloat;?>" />
<input type="hidden" name="pio" value="<?php echo $valio;?>" />
</form>
<?php
$ismysql = (false !== function_exists("mysql_query"))?"":" disabled";
$ismail = (false !== function_exists("mail"))?"":" disabled";
?>
<br />
<form action="<?php echo phpself."?action=testinfo&icon=$icon&#testinfo2"?>" method="post">
<div id="testinfo2">
<!--mysql数据库连接检测--><br /><br />
<table cellpadding="0" cellspacing="0" border="0" width="80%" class="ctable" id="otable2" align="center">
<tr><th colspan="2">mysql数据库连接检测</th></tr>
<tr>
 <td width="30%" align="right">mysql服务器:</td>
 <td><input name="mysqlhost" type="text" id="mysqlhost" value="localhost" size="80" class="text2" /></td>
</tr>
<tr>
 <td align="right">mysql用户名:</td>
 <td><input name="mysqluser" type="text" id="mysqluser" value="root" size="80" class="text2" /></td>
</tr>
<tr>
 <td align="right">mysql用户密码:</td>
 <td><input type="text" name="mysqlpassword" <?php echo $ismysql?> size="80" class="text2" /></td>
</tr>
<tr>
 <td align="right">mysql数据库名称:</td>
 <td><input type="text" name="mysqldb" size="80" class="text2" /></td>
</tr>
<tr>
 <td align="right" height="30"><input type="submit" class="button" value="connect" <?php echo $ismysql?>  name="act" /></td>
    <td><div class="check00"><?php if("show"==$mysqlreshow){echo $mysqlre;}?> </div></td>
</tr>
</table>
</div>
</form>
<br /><br />
<form action="<?php echo phpself."?action=testinfo&icon=$icon&#testinfo5"?>" method="post">
<div id="testinfo5">
<!--邮件发送检测-->
<table cellpadding="0" cellspacing="0" border="0" width="80%" class="ctable" id="otable3" align="center">
<tr><th colspan="2">php配置参数状况</th></tr>
<tr>
    <td width="30%" align="right">请输入您要检测的参数名称:</td>
    <td><input type="text" name="opname" size="80" class="text2" /></td>
</tr>
<tr>
    <td align="right" height="30"><input type="submit" class="button" value="configuration_check" name="act" /></td>
    <td><div class="check00"><?php if("show"==$opreshow){echo $opre;}?></div></td>
</tr>
</table>
</div>
</form>
<br /><br />
<form action="<?php echo phpself."?action=testinfo&icon=$icon&#testinfo3"?>" method="post">
<div id="testinfo3">
<!--函数检测-->
<table cellpadding="0" cellspacing="0" border="0" width="80%" class="ctable" id="otable4" align="center">
<tr><th colspan="2">函数检测</th></tr>
<tr>
 <td width="30%" align="right">请输入您要检测的函数名称:</td>
 <td><input type="text" name="funname" size="80" class="text2" /></td>
</tr>
<tr>
 <td align="right" height="30"><input type="submit" class="button" value="function_check" name="act" /></td>
    <td><div class="check00"><?php if("show"==$funreshow){echo $funre;}?> </div></td>
</tr>
</table>
</div>
</form>
<br /><br />
<form action="<?php echo phpself."?action=testinfo&icon=$icon&#testinfo4"?>" method="post">
<div id="testinfo4">
<!--邮件发送检测-->
<table cellpadding="0" cellspacing="0" border="0" width="80%" class="ctable" id="otable5" align="center">
<tr><th colspan="2">mail邮件发送测试</th></tr>
<tr>
    <td width="30%" align="right">请输入您要检测的邮件地址:</td>
    <td><input type="text" name="mailreceiver" size="80" <?php echo $ismail?> class="text2" /></td>
</tr>
<tr>
    <td align="right" height="30"><input type="submit" class="button" value="sendmail" <?php echo $ismail?>  name="act" /></td>
    <td><div class="check00"><?php if("show"==$mailreshow){echo $mailre;}?></div></td>
</tr>
</table>
</div>
</form>

<?php
  break;
}?>
</div>

</div>
</body>
</html>
<?php

/*=============================================================
    函数库
=============================================================*/

/*-------------------------------------------------------------------------------------------------------------
    检测函数支持
--------------------------------------------------------------------------------------------------------------*/
    function isfun($funname)
    {
        return (false !== function_exists($funname))?yes:no;
    }
/*-------------------------------------------------------------------------------------------------------------
    检测php设置参数
--------------------------------------------------------------------------------------------------------------*/
    function getcon($varname)
    {
        switch($res = get_cfg_var($varname))
        {
            case 0:
            return no;
            break;
            case 1:
            return yes;
            break;
            default:
            return $res;
            break;
        }
        
    }
/*-------------------------------------------------------------------------------------------------------------
    整数运算能力测试
--------------------------------------------------------------------------------------------------------------*/
    function test_int()
    {
        $timestart = gettimeofday();
        for($i = 0; $i <= 3000000; $i++);
        {
            $t = 1+1;
        }
        $timeend = gettimeofday();
        $time = ($timeend["usec"]-$timestart["usec"])/1000000+$timeend["sec"]-$timestart["sec"];
        $time = round($time, 6)."秒";
        return $time;
    }
/*-------------------------------------------------------------------------------------------------------------
    浮点运算能力测试
--------------------------------------------------------------------------------------------------------------*/
    function test_float()
    {
        $t = pi();
        $timestart = gettimeofday();
        for($i = 0; $i < 3000000; $i++);
        {
            sqrt($t);
        }
        $timeend = gettimeofday();
        $time = ($timeend["usec"]-$timestart["usec"])/1000000+$timeend["sec"]-$timestart["sec"];
        $time = round($time, 6)."秒";
        return $time;
    }
/*-------------------------------------------------------------------------------------------------------------
    数据io能力测试
--------------------------------------------------------------------------------------------------------------*/
    function test_io()
    {
        $fp = @fopen(phpself, "r");
        $timestart = gettimeofday();
        for($i = 0; $i < 10000; $i++)
        {
            @fread($fp, 10240);
            @rewind($fp);
        }
        $timeend = gettimeofday();
        @fclose($fp);
        $time = ($timeend["usec"]-$timestart["usec"])/1000000+$timeend["sec"]-$timestart["sec"];
        $time = round($time, 6)."秒";
        return($time);
    }
/*-------------------------------------------------------------------------------------------------------------
    比例条
--------------------------------------------------------------------------------------------------------------*/
    function bar($percent)
    {
    echo '<br/><ul class="bar">
 <li style="width:';
 echo $percent."%">";
    echo '&nbsp;</li>
    </ul>';
}
/*-------------------------------------------------------------------------------------------------------------
    根据不同系统取得cpu相关信息
--------------------------------------------------------------------------------------------------------------*/
 switch(php_os) {
  case "linux":
   $sysreshow = (false !== ($sysinfo = sys_linux()))?"show":"none";
   break;
  case "freebsd":
   $sysreshow = (false !== ($sysinfo = sys_freebsd()))?"show":"none";
   break;
  case "winnt":
   $sysreshow = (false !== ($sysinfo = sys_windows()))?"show":"none";
   break;
  default:
   break;
 }

/*-------------------------------------------------------------------------------------------------------------
    系统参数探测 linux
--------------------------------------------------------------------------------------------------------------*/
    function sys_linux()
    {
        // cpu
        if (false === ($str = @file("/proc/cpuinfo"))) return false;
        $str = implode("", $str);
        @preg_match_all("/models+names{0,}:+s{0,}([ws)(.]+)[ ]+/", $str, $model);
        //@preg_match_all("/cpus+mhzs{0,}:+s{0,}([d.]+)[ ]+/", $str, $mhz);
        @preg_match_all("/caches+sizes{0,}:+s{0,}([d.]+s{0,}[a-z]+[ ]+)/", $str, $cache);
        if (false !== is_array($model[1]))
            {
            $res['cpu']['num'] = sizeof($model[1]);
            for($i = 0; $i < $res['cpu']['num']; $i++)
            {
                $res['cpu']['detail'][] = "类型:".$model[1][$i]." 缓存:".$cache[1][$i];
            }
            if (false !== is_array($res['cpu']['detail'])) $res['cpu']['detail'] = implode("<br />", $res['cpu']['detail']);
            }
        
        // uptime
        if (false === ($str = @file("/proc/uptime"))) return false;
        $str = explode(" ", implode("", $str));
        $str = trim($str[0]);
        $min = $str / 60;
        $hours = $min / 60;
        $days = floor($hours / 24);
        $hours = floor($hours - ($days * 24));
        $min = floor($min - ($days * 60 * 24) - ($hours * 60));
        if ($days != 0) {$res['uptime'] = $days."天";}
        if ($hours != 0) {$res['uptime'] .= $hours."小时";}
        $res['uptime'] .= $min."分钟";
        
        // memory
        if (false === ($str = @file("/proc/meminfo"))) return false;
        $str = implode("", $str);
        preg_match_all("/memtotals{0,}:+s{0,}([d.]+).+?memfrees{0,}:+s{0,}([d.]+).+?swaptotals{0,}:+s{0,}([d.]+).+?swapfrees{0,}:+s{0,}([d.]+)/s", $str, $buf);
        
        $res['memtotal'] = round($buf[1][0]/1024, 2);
        $res['memfree'] = round($buf[2][0]/1024, 2);
        $res['memused'] = ($res['memtotal']-$res['memfree']);
        $res['mempercent'] = (floatval($res['memtotal'])!=0)?round($res['memused']/$res['memtotal']*100,2):0;
        
        $res['swaptotal'] = round($buf[3][0]/1024, 2);
        $res['swapfree'] = round($buf[4][0]/1024, 2);
        $res['swapused'] = ($res['swaptotal']-$res['swapfree']);
        $res['swappercent'] = (floatval($res['swaptotal'])!=0)?round($res['swapused']/$res['swaptotal']*100,2):0;
        
        // load avg
        if (false === ($str = @file("/proc/loadavg"))) return false;
        $str = explode(" ", implode("", $str));
        $str = array_chunk($str, 3);
        $res['loadavg'] = implode(" ", $str[0]);
        
        return $res;
    }
/*-------------------------------------------------------------------------------------------------------------
    系统参数探测 freebsd
--------------------------------------------------------------------------------------------------------------*/
    function sys_freebsd()
    {
        //cpu
        if (false === ($res['cpu']['num'] = get_key("hw.ncpu"))) return false;
        $res['cpu']['detail'] = get_key("hw.model");
        
        //load avg
        if (false === ($res['loadavg'] = get_key("vm.loadavg"))) return false;
        $res['loadavg'] = str_replace("{", "", $res['loadavg']);
        $res['loadavg'] = str_replace("}", "", $res['loadavg']);
        
        //uptime
        if (false === ($buf = get_key("kern.boottime"))) return false;
        $buf = explode(' ', $buf);
        $sys_ticks = time() - intval($buf[3]);
        $min = $sys_ticks / 60;
        $hours = $min / 60;
        $days = floor($hours / 24);
        $hours = floor($hours - ($days * 24));
        $min = floor($min - ($days * 60 * 24) - ($hours * 60));
        if ($days != 0) $res['uptime'] = $days."天";
        if ($hours != 0) $res['uptime'] .= $hours."小时";
        $res['uptime'] .= $min."分钟";
        
        //memory
        if (false === ($buf = get_key("hw.physmem"))) return false;
        $res['memtotal'] = round($buf/1024/1024, 2);
        $buf = explode(" ", do_command("vmstat", ""));
        $buf = explode(" ", trim($buf[2]));
        
        $res['memfree'] = round($buf[5]/1024, 2);
        $res['memused'] = ($res['memtotal']-$res['memfree']);
        $res['mempercent'] = (floatval($res['memtotal'])!=0)?round($res['memused']/$res['memtotal']*100,2):0;
          
        $buf = explode(" ", do_command("swapinfo", "-k"));
        $buf = $buf[1];
        preg_match_all("/([0-9]+)s+([0-9]+)s+([0-9]+)/", $buf, $bufarr);
        $res['swaptotal'] = round($bufarr[1][0]/1024, 2);
        $res['swapused'] = round($bufarr[2][0]/1024, 2);
        $res['swapfree'] = round($bufarr[3][0]/1024, 2);
        $res['swappercent'] = (floatval($res['swaptotal'])!=0)?round($res['swapused']/$res['swaptotal']*100,2):0;
        
        return $res;
    }
    
/*-------------------------------------------------------------------------------------------------------------
    取得参数值 freebsd
--------------------------------------------------------------------------------------------------------------*/
function get_key($keyname)
    {
        return do_command('sysctl', "-n $keyname");
    }
    
/*-------------------------------------------------------------------------------------------------------------
    确定执行文件位置 freebsd
--------------------------------------------------------------------------------------------------------------*/
    function find_command($commandname)
    {
        $path = array('/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
        foreach($path as $p)
        {
            if (@is_executable("$p/$commandname")) return "$p/$commandname";
        }
        return false;
    }
    
/*-------------------------------------------------------------------------------------------------------------
    执行系统命令 freebsd
--------------------------------------------------------------------------------------------------------------*/
    function do_command($commandname, $args)
    {
        $buffer = "";
        if (false === ($command = find_command($commandname))) return false;
        if ($fp = @popen("$command $args", 'r'))
            {
    while (!@feof($fp))
    {
     $buffer .= @fgets($fp, 4096);
    }
    return trim($buffer);
   }
        return false;
    }

/*-------------------------------------------------------------------------------------------------------------
    系统参数探测 windows
--------------------------------------------------------------------------------------------------------------*/
    function sys_windows()
 {
 //$phpos=php_os;
 $sysinfo['uptime'] ="对不起windows系统不支持";
 
 }
?>

这是一款php分页代码是一款经典的可自动判断url参数再进行分页的实例代码,同时自定了上页,下页,首页,尾页哦,同时还支持select下拉框跳转代码。
 代码如下 复制代码

class page{

 var $page_name="page";

 var $next_page='下一页';

 var $pre_page='上一页';

 var $first_page='首页';

 var $last_page='尾页';

 var $pre_bar='<<';

 var $next_bar='>>';

 var $format_left='';

 var $format_right='';

 var $pagebarnum=5;

 var $totalpage=0;

 var $nowindex=1;

 var $url="";

 var $offset=0;

 var $rewrite = array();


 function page($array)

 {

  if(is_array($array)){

     if(!array_key_exists('total',$array))$this->error(__function__,'need a param of total');

     $total=intval($array['total']);

     $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;

     $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';

     $url=(array_key_exists('url',$array))?$array['url']:'';

     $action = (array_key_exists('action', $array)) ? $array['action'] : '';
     $id0 = (array_key_exists('id0', $array)) ? $array['id0'] : '';
  $id1 = (array_key_exists('id1', $array)) ? $array['id1'] : '';
  $id2 = (array_key_exists('id2', $array)) ? $array['id2'] : '';
  $id3 = (array_key_exists('id3', $array)) ? $array['id3'] : '';

  }else{

     $total=$array;

     $perpage=10;

     $nowindex='';

     $url='';

     $action = '';
     $id0 = '';
     $id1 = '';
     $id2 = '';
  $id3 = '';

  }

  if((!is_int($total))||($total<0))$this->error(__function__,$total.' is not a positive integer!');

  if((!is_int($perpage))||($perpage<=0))$this->error(__function__,$perpage.' is not a positive integer!');

  if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);

  $this->_set_nowindex($nowindex);

  $this->_set_url($url);

  $this->totalpage=ceil($total/$perpage);

  $this->offset=($this->nowindex-1)*$perpage;

  $this->action = $action;
  $this->rewrite = array('action'=>$action,'id0'=>$id0, 'id1'=>$id1, 'id2'=>$id2, 'id3'=>$id3);
 }

 function set($var,$value)
 {

  if(in_array($var,get_object_vars($this)))

     $this->$var=$value;

  else {

   $this->error(__function__,$var." does not belong to pb_page!");

  }

 }

 function next_page($style=''){
  if($this->nowindex<$this->totalpage){
  return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
 }
 return '<a class="'.$style.'">'.$this->next_page.'</a>';
 }

 function pre_page($style=''){
  if($this->nowindex>1){
    return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
  }
  return '<a class="'.$style.'">'.$this->pre_page.'</a>';
 }

 function first_page($style=''){
  if($this->nowindex==1){
     return '<a class="'.$style.'">'.$this->first_page.'</a>';
  }
  return $this->_get_link($this->_get_url(1),$this->first_page,$style);
 }

 function last_page($style=''){
  if($this->nowindex==$this->totalpage||$this->totalpage==0){

      return '<a class="'.$style.'">'.$this->last_page.'</a>';

  }

  return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);

 }


 function nowbar($style='',$nowindex_style='')

 {

  $plus=ceil($this->pagebarnum/2);

  if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);

  $begin=$this->nowindex-$plus+1;

  $begin=($begin>=1)?$begin:1;

  $return='';

  for($i=$begin;$i<$begin+$this->pagebarnum;$i++)

  {

   if($i<=$this->totalpage){

    if($i!=$this->nowindex)

        $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));

    else

        $return.=$this->_get_text('<a class="'.$nowindex_style.'">'.$i.'</a>');

   }else{

    break;

   }

   $return.=" ";

  }

  unset($begin);

  return $return;

 }

 /**

  * 获取显示跳转按钮的代码

  *

  * @return string

  */

 function select()

 {

   $return='<select name="pb_page_select">';

  for($i=1;$i<=$this->totalpage;$i++)

  {

   if($i==$this->nowindex){

    $return.='<option value="'.$i.'" selected>'.$i.'</option>';

   }else{

    $return.='<option value="'.$i.'">'.$i.'</option>';

   }

  }

  unset($i);


  $return.='</select>';

  return $return;

 }

 

 /**

  * 获取mysql教程 语句中limit需要的值

  *

  * @return string

  */

 function offset()

 {

  return $this->offset;

 }

 

 /**

  * 控制分页显示风格(你可以增加相应的风格)

  *

  * @param int $mode

  * @return string

  */

 function show($mode=1)

 {

  switch ($mode)

  {

   case '1':

    $this->next_page='下一页';

    $this->pre_page='上一页';

    return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';

    break;

   case '2':

    $this->next_page='下一页';

    $this->pre_page='上一页';

    $this->first_page='首页';

    $this->last_page='尾页';

    return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';

    break;

   case '3':

    $this->next_page='下一页';

    $this->pre_page='上一页';

    $this->first_page='首页';

    $this->last_page='尾页';

    return $this->first_page("page_box")."".$this->pre_page("page_box")."".$this->nowbar("page_box_a","page_box_b")."".$this->next_page("page_box")."".$this->last_page("page_box")."<a class="clear"></a>";

    break;

   case '4':

    $this->next_page='下一页';

    $this->pre_page='上一页';

    return $this->pre_page().$this->nowbar().$this->next_page();

    break;

   case '5':

    return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();

    break;

  }

 

 }

 function _set_url($url="")

 {

  if(!empty($url)){

   $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";

  }else{

   if(empty($_server['query_string'])){

    $this->url=$_server['request_uri']."?".$this->page_name."=";

   }else{

    if(stristr($_server['query_string'],$this->page_name.'=')){

     $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_server['request_uri']);

     $last=$this->url[strlen($this->url)-1];

     if($last=='?'||$last=='&'){

         $this->url.=$this->page_name."=";

     }else{

         $this->url.='&'.$this->page_name."=";

     }

    }else{

     $this->url=$_server['request_uri'].'&'.$this->page_name.'=';

    }

   }

  }

 }


 function _set_nowindex($nowindex)

 {

  if(empty($nowindex)){

   if(isset($_get[$this->page_name])){

    $this->nowindex=intval($_get[$this->page_name]);

   }

  }else{

   $this->nowindex=intval($nowindex);

  }

 }

 function _get_url($pageno=1)
 {
  global $_cfg;
  $arr = $this->rewrite;
 //print_r($arr);
 //print_r($this->url.$pageno);
  if($_cfg['urlrewrite'] && !empty($arr['action'])){
   return url_rewrite($arr['action'], array('id0'=>$arr['id0'],'id1'=>$arr['id1'],'id2'=>$arr['id2'],'id3'=>$arr['id3'],'page'=>$pageno));
  }else{
   return $this->url.$pageno;
  }

 }

 function _get_text($str)

 {

  return $this->format_left.$str.$this->format_right;

 }

 function _get_link($url,$text,$style=''){

  $style=(empty($style))?'':'class="'.$style.'"';

  return '<a '.$style.' href="'.$url.'">'.$text.'</a>';

 }


 function error($function,$errormsg)

 {

     die('error in file <b>'.__file__.'</b> ,function <b>'.$function.'()</b> :'.$errormsg);

 }

}
?>

 

 代码如下 复制代码

用法:
application('key','value'); //设置 key=value
$value = application('key'); //获取 key的值
*/

function application()
{
 $args = func_get_args(); //获取输入参数
 if (count($args) >2 || count($args) < 1) return;
 $ssid = session_id(); //保存当前session_id
 session_write_close(); //结束当前session
 ob_start(); //禁止全局session发送header
 session_id("xxx"); //注册全局session_id
 session_start(); //开启全局session
 $key = $args[0];
 if (count($args) == 2) //如果有第二个参数,那么表示写入全局session
 {
  $re = ($_session[$key] = $args[1]);
 }
 else // 如果只有一个参数,那么返回该参数对应的value
 {
  $re = $_session[$key];
 }
 session_write_close(); //结束全局session
 session_id($ssid); //重新注册上面被中断的非全局session
 session_start(); //重新开启
 ob_end_clean(); //抛弃刚刚由于session_start产生的一些header输出
 return $re;
}

 

下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些。

<?
//php教程 中文字符串截取函数
/*

*/

 代码如下 复制代码
function substr($str = '', $offset = 0, $len = 0){
    $len || ($len = strlen($str));
    preg_match_all('/./us', $str, $result);
    return implode('', array_slice($result[0], $offset, $len));
}

//方法二

 代码如下 复制代码

if (!function_exists('mb_substr')) {
function mb_substr($str, $start, $len = '', $encoding="utf-8"){
  $limit = strlen($str);

  for ($s = 0; $start > 0;--$start) {// found the real start
    if ($s >= $limit)
      break;

    if ($str[$s] <= "")
      ++$s;
    else {
      ++$s; // skip length

      while ($str[$s] >= "€" && $str[$s] <= "�")
        ++$s;
    }
  }

  if ($len == '')
    return substr($str, $s);
  else
    for ($e = $s; $len > 0; --$len) {//found the real end
      if ($e >= $limit)
        break;

      if ($str[$e] <= "")
        ++$e;
      else {
        ++$e;//skip length

        while ($str[$e] >= "€" && $str[$e] <= "�" && $e < $limit)
          ++$e;
      }
    }

  return substr($str, $s, $e - $s);
}
}

?>

[!--infotagslink--]

相关文章

  • 使用C# CefSharp Python采集某网站简历并且自动发送邀请短信的方法

    这篇文章主要给大家介绍了关于如何使用C# CefSharp Python采集某网站简历并且自动发送邀请短信的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧...2020-06-25
  • 微信小程序 天气预报开发实例代码源码

    这篇文章主要介绍了微信小程序 天气预报开发实例代码源码的相关资料,这里含有源码,需要的朋友可以参考下...2017-01-23
  • asp.net采集网页图片的具体方法

    采集网页上图片的主要关键是在怎么解析出页面代码里那些img标签的src属性...2021-09-22
  • 音乐采集程序

    <?php header("Content-Type: text/html; charset=gb2312"); session_start(); set_time_limit(0); $str = file_get_contents('http://music.soso.com/'); pr...2016-11-25
  • java如何根据IP获取当前区域天气信息详解

    根据IP自动获取当地的天气预报信息这个功能大家应该都遇到过,天气预报信息用途非常广泛,篇文章主要给大家介绍了关于java如何根据IP获取当前区域天气信息的相关资料,需要的朋友可以参考下...2021-08-03
  • 利用fopen函数采集新闻页面内容保存到本地函数

     <? / / PHP的新闻抓取由Neil Moomey,。 / /你可以自由的使用此代码作为您的愿望。 / /请确保您可以从任何网站,你抓从标题许可。 / /你可能需要写上您的服务器上...2016-11-25
  • php几种采集远程服务器内容代码

    //方法一模仿用户访问网页 代码如下 复制代码 function readpr($link,$url) { $fp = fsockopen ($url, 80, $errno, $errstr, 30); if (!...2016-11-25
  • PHP采集远程图片到本地实现代码

    在php中要保存远程图片到自己服务器本地,我们需要先正则字符串中的内容图片,然后再利用相关函数把图片读取并保存到本地硬盘即可。 代码如下 复制代码 ...2016-11-25
  • Php CURL模拟登陆论坛并采集数据实例

    本文章来给各位同学介绍一下关于Php CURL模拟登陆论坛并采集数据实例,如果你对利用curl模拟登录功能有兴趣可进入参考。 要模拟浏览器访问网站,首选要学会观察浏览...2016-11-25
  • 如何采集搜狗微信搜索的内容(SogouEncrypt版) -- hzw

    在搜狗微信搜索中,之前微信的内容是用 http://weixin.sogou.com/gzhjs?cb=sogou.weixin.gzhcb&openid=oIWsFt1OaL2XHiDx6809O8q2KZ5A&page=1 这类格式来调用数据的,openid...2016-05-19
  • 搭建海量数据采集爬虫框架教程

    海量数据采集爬虫,相当于大的搜索引擎爬虫了,现在我们来看看如何搭建海量数据采集爬虫框架,相关的朋友可以参考一下。 随着BIG DATA大数据概念逐渐升温,如何搭建一个...2016-11-25
  • apache如何禁止网络爬虫采集的配置方法

    Apache中禁止网络爬虫,其实也挺简单的,只要把下面的代码配置到apache的httpd.conf文件中的Location中,就可以了。 <Location /> SetEnvIfNoCase User-Agent "spider" bad_b...2016-01-28
  • DEDE采集大师官方留后门的删除办法

    <?php教程 require_once(dirname(__file__)."/../include/common.inc.php"); if(emptyempty($dopost)) { $dopost = ""; } if($dopost=="rename") {...2016-11-25
  • C# 利用AForge实现摄像头信息采集

    这篇文章主要介绍了C# 如何利用AForge实现摄像头信息采集,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-11-03
  • 防采集方法

    这个防采集的方法是我今天在无奈之下想出来的,要开网站一看我晕不能访问,立马跑到服务器上一看,apache点N高的内存,后来在一急之下就想出了这种办法,呵呵,到底是什么方...2017-07-06
  • 网站内容采集到底有没有用

    经过百度的几次小更新,收录并不乐观,一直才10多篇的样子,就连google收录都不怎么样,自己对于网站的收录还是很看重的,但是最终的结果却不令我乐观,自己想了想原因,应该就是因...2017-07-06
  • PHP html dom php+正则 采集文章代码

    <?php //包含PHP Simple html Dom 类库文件 include_once('./simplehtmldom/simple_html_dom.php'); //采集html function getwebcontent($url){ $ch = cu...2016-11-25
  • php 网页采集入库程序代码

    网页采集现在用到最多是工具了,像最受站长欢迎的就是火车头了,但有一些站长喜欢使用网页来自定义采集了,下面一起来看一个php 网页采集入库程序代码 php 网页采集程...2016-11-25
  • PHP命令行采集所有股票趋势信息程序

    股票信息我们做理财网站都只有采集门户站的数据不可能自己生成股票信息了,这个就会要用到抓取股票站的数据了,下面我们来看一篇关于PHP命令行采集所有股票趋势信息程序...2016-11-25
  • asp.net实现根据城市获取天气预报的方法

    这篇文章主要介绍了asp.net实现根据城市获取天气预报的方法,涉及asp.net调用新浪接口获取天气预报信息的实现技巧,非常简单实用,需要的朋友可以参考下...2021-09-22