ip2long: 让ip直接序列化成数字。好做统计用。
long2ip: 反过来
var_export 输出原样的php代码
例:
以下代码:
$a = array (1, 2, array (“a”, “b”, “c”));
var_export ($a);
/* 输出:
array (
0 => 1,
1 => 2,
2 =>
array (
0 => ‘a’,
1 => ‘b’,
2 => ‘c’,
),
)
*/
我想输出成array (0 => 1,1 => 2,2 => array (0 => ‘a’,1 => ‘b’,2 => ‘c’,),)
解:
$a = array (1, 2, array (“a”, “b”, “c”));
$str = var_export ($a, 1); // 将 var_export 的结果作为函数返回值,而不是直接输出
echo str_replace(“\n”, “”, $str); // 替换换行,然后输出~
用例:
php生成 特定的 config 文件
[code lang=”php”]
function createcacheconfig(){
$input = new Input();
$check_code = $input->get(‘check_code’,’trim’);
if($check_code==’welcomecityofsteam’){
$newc = M("Config")->field("name,status")->select();
$conf = array();
foreach ($newc as $key=>$row){
$conf[$row[‘name’]] = $row[‘status’];
}
$var = var_export($conf,true);
$logStr = "";
$logFd = fopen(CONF_PATH."/ad_config.php", "w");
$logStr = "<?php\n return $var;\n?>";
fwrite($logFd, $logStr);
fclose($logFd);
$result = M(‘ServerList’)->field("id,area_name,server_name,server_id,server_ip,server_version,server_clienturl,socket_port,kehud_vistion,fud_vistion,yunwei")->order("server_id asc")->select();
$cellstr = array();
foreach($result as $row) {
foreach ($row as $key => $value) {
$cellstr[$row[‘server_id’]][$key] =$value?$value:”;
}
}
$var = var_export($cellstr,true);
$logStr = "";
$logFd = fopen(CONF_PATH."/server_config.php", "w");
$logStr = "<?php\n return $var;\n?>";
fwrite($logFd, $logStr);
fclose($logFd);
}
}
[/code]