实际开发过程中的公共函数库分享
包含一下功能
/*
* function getClientSysLanguage() // 设定系统语言
* function setCookieForever($cookie_name,$var) // set cookie forever
* function getStringSupReplace($strInput) // 格式化字符串
* getCodepage($codepage_id) // 这部分衔接js中的判断00的处理方法
* convertlang2code($str) // Pls add here by Hanic.Wang
* redirect($page) // 跳转函数
* serialize_($str)// 将数组线性化用encode保存
* unserialize_($str)// 将str decode解码然后解压成数组
* reArrangedIndex($arr)把一个数组的序号重新按0,1,2,3顺序排列
* getInt($data3,$d) //取整
* preg_match_define($str,$spilit) //截取某字符之前的所有字符,没有的话返回原字符串
* addstrlen($a,$c=2,$addstr="0")当数据不满可以用$addstr填满长度 比如 234 需要 5位 $addstr=0 则返回00234
* getDateNow()返回当前时间 2008-03-09
* getInt($data3,$d)取整 3.1 返回4
<?php
/*
* function getClientSysLanguage() // 设定系统语言
* function setCookieForever($cookie_name,$var) // set cookie forever
* function getStringSupReplace($strInput) // 格式化字符串
* getCodepage($codepage_id) // 这部分衔接js中的判断00的处理方法
* convertlang2code($str) // Pls add here by Hanic.Wang
* redirect($page) // 跳转函数
* serialize_($str)// 将数组线性化用encode保存
* unserialize_($str)// 将str decode解码然后解压成数组
* reArrangedIndex($arr)把一个数组的序号重新按0,1,2,3顺序排列
* getInt($data3,$d) //取整
* preg_match_define($str,$spilit) //截取某字符之前的所有字符,没有的话返回原字符串
* addstrlen($a,$c=2,$addstr="0")当数据不满可以用$addstr填满长度 比如 234 需要 5位 $addstr=0 则返回00234
* getDateNow()返回当前时间 2008-03-09
* getInt($data3,$d)取整 3.1 返回4
*/
// 设定系统语言 返回 en/gb/big5
function getClientSysLanguage($post_sys_lang,$cookie_dreye_sys_lang){
$sys_lang = $post_sys_lang?$post_sys_lang:$cookie_dreye_sys_lang;
if ($sys_lang){
if ($POST['sys_lang']) setCookieForever("dreye[sys_lang]",$sys_lang);
$lang_id = $sys_lang;
} else {
$client_sys_language = $_SERVER["HTTP_ACCEPT_LANGUAGE"]; // system language
$lang_id = DEFAULT_LANGUAGE; // default gb
if (preg_match ("/^zh-cn/i",$client_sys_language) || preg_match ("/^zh-sg/i",$client_sys_language)){
$lang_id = "gb"; //gb2312
}
if (preg_match ("/^zh-tw/i",$client_sys_language) || preg_match ("/^zh-hk/i",$client_sys_language)){
$lang_id = "big5"; //big5
}
if (preg_match ("/^en/i",$client_sys_language)){
$lang_id = "en"; // en
}
}
setCookieForever("dreye[sys_lang]",$lang_id);
return $lang_id;
}
// set cookie forever
function setCookieForever($cookie_name,$var){
//echo COOKIE_END_YEAR . "#" . $cookie_name . "#" . $var;
setcookie($cookie_name,$var,mktime(0,0,0,1,1,COOKIE_END_YEAR));
}
//格式化字符串 @param strInput like:test@1 @return String like:test<sup>1</sup>
function getStringSupReplace($strInput){ /**转换时请考虑优先级**/
$str_array_find = array("@10","@11","@12","@13","@14","@15","@1","@2","@3","@4","@5","@6","@7","@8","@9");
$str_array_repl = array ("<sup>10</sup>","<sup>11</sup>","<sup>12</sup>","<sup>13</sup>","<sup>14</sup>","<sup>15</sup>","<sup>1</sup>","<sup>2</sup>","<sup>3</sup>","<sup>4</sup>","<sup>5</sup>","<sup>6</sup>","<sup>7</sup>","<sup>8</sup>","<sup>9</sup>");
$strInput = $strInput? str_replace($str_array_find,$str_array_repl,$strInput): $strInput;
return $strInput;
}
function getString_SupReplace($strInput){ /**转换时请考虑优先级**/
$str_array_repl = array("@10","@11","@12","@13","@14","@15","@1","@2","@3","@4","@5","@6","@7","@8","@9");
$str_array_find = array ("<sup>10</sup>","<sup>11</sup>","<sup>12</sup>","<sup>13</sup>","<sup>14</sup>","<sup>15</sup>","<sup>1</sup>","<sup>2</sup>","<sup>3</sup>","<sup>4</sup>","<sup>5</sup>","<sup>6</sup>","<sup>7</sup>","<sup>8</sup>","<sup>9</sup>");
$strInput = $strInput? str_replace($str_array_find,$str_array_repl,$strInput): $strInput;
return $strInput;
}
//这部分衔接js中的判断00的处理方法
function getCodepage($codepage_id){
global $global_code_page;
switch ($codepage_id){
case '01':break;
case '02':break;
case '03':break;
case '04':break;
case '00':// 不能得到确切判断,结合系统语言判断
$codepage_id = $global_code_page; //default
$sys_lang = $_COOKIE['dreye']['sys_lang']?$_COOKIE['dreye']['sys_lang']:$default_sys_lang;
$codepage_id = convertlang2code($_COOKIE['dreye']['sys_lang']);
break;
default://die("getCodePage:: Error codepage from form");
break;
}
return $codepage_id;
}
//
function convertlang2code($str){
switch ($str){
case 'big5': $code = "03";break;
case 'gb': $code = "02";break;
case 'en': $code = "03";break;
case 'jp': $code = "03" ;break;
default: die("convertlang2code:: error str"); break;
}
return $code;
}
// 跳转函数
function redirect($page){
global $global_url;
if ($global_url){
header("Location:$global_url");
exit;
} else {
die ("Redirect :: Error - no global url");
}
}
// 将数组线性化用encode保存
function serialize_($arr){
if (is_array($arr) && !empty($arr)){
$data = base64_encode(serialize($arr));
}
return $data;
}
// 将str decode解码然后解压成数组
function unserialize_($str){
if ($str && !empty($str)){
$arr = unserialize(base64_decode($str));
}
return $arr;
}
//把一个数组的序号重新按0,1,2,3顺序排列
function reArrangedIndex($arr){
if (is_array($arr) && !empty($arr)){
$array = array_reverse(array_reverse($arr));
}
return $array;
}
// 将$array1中第一个值抛出数组
function popTopElementFromArray($array1){
/*
global $global_history_length;
$length = count($array1);
if ($length > $global_history_length){
*/
$array1 = array_reverse($array1);
array_pop($array1);
$array1 = array_reverse($array1);
return $array1;
}
//给一个数组的最后插入一个元素
function addone($stack,$str){
if (!(is_array($stack)) || empty($stack)){
$stack = array($str);
} else {
array_push($stack,$str);
}
return $stack;
}
//去除$array1中的$str值
function deleteStrFromArray($array1,$str){
$array2 = array($str);
if ((!is_array($array1))||empty($array1)){
$result = $array1;
}else{
$result = array_diff($array1, $array2);
}
return $result;
}
//preg_match_define($str,$spilit)截取某字符之前的所有字符,没有的话返回原字符串
function preg_match_define($str,$spilit){
$str_r = explode($spilit,$str);
return $str_r[0] ;
}
function returnArray($word_show_list,$list_num){
if (count($word_show_list) > $list_num){
$i = 1;
foreach ($word_show_list as $key => $value){
if ($i>$list_num){break;}
$result[$key] = $value;
$i=$i+1;
}
} else {
$result = $word_show_list;
}
return $result;
}
//getInt($data3,$d)取整
function getInt($data3,$d){
if ($data3 && $d ) {
$page = intval($data3/$d);
if ($page *$d < $data3 ){
$page = $page+1;
}
} else {
die("function: getInt() error input");
}
return $page;
}
function getHelpInfo($filename,$project){
global $nd_public;
global $nd_search;
if ($filename == "index.php"){
$id = rand(0,(count($nd_public)-1));
$result = $nd_public[$id];
}
if ($filename == "dict.php"||$filename == "history.php"){
$id = rand(0,(count($nd_search)-1));
$result = $nd_search[$id];
}
return $result;
}
function xiegangZhuanyi($str){
$result = str_replace(array("/'","///""),array("'","/""),$str);
return $result;
}
// 支持截取中文
function SubTitle($title, $length) {
if (strlen($title)>$length) {
$temp = 0;
for($i=0; $i<$length; $i++){
if (ord($title[$i]) > 128){
$temp++;
}
if ($temp%2 == 0){
$title = substr($title,0,$length);
}else{
$title = substr($title,0,$length-1);
}
}
$title .="...";
}
return $title;
}
// getDateNow()返回当前时间 2008-03-09
function getDateNow(){
$today = getdate();
$ds_date = $today['year'] . "-" . addstrlen($today['mon'],2,"0") . "-" . addstrlen($today['mday'],2,"0");
return $ds_date;
}
// addstrlen($a,$c=2,$addstr="0")当数据不满可以用$addstr填满长度 比如 234 需要 5位 $addstr=0 则返回00234
function addstrlen($a,$c=2,$addstr="0"){
$b = strlen($a);
if ($b < $c) {
for ($i=0;$i<$c-$b;$i++){
$t .= $addstr;
}
}
$result = strval($t . $a);
return $result;
}
?>