PHP開發中經常用到的一些函數
來源:互聯網
上載者:User
<?
class useful{
/*
* 常用函數類
* 作 者:多菜鳥
* 聯絡郵箱:kingerq AT msn DOT com
* 建立時間:2005-07-18
* 來源:http://blog.csdn.net/kingerq
*/
/*
* 功能:格式化數字,以標準MONEY格式輸出
*/
function formatnumber($num){
return number_format($num, 2, ".", ",");
}
/*
* 功能:格式化文本,將\n轉成<br>等
* 參數:$string 來源字串
* 返回:處理後的字串
*/
function formatstring($string = ""){
$string = preg_replace(array("/ /", "/ /"), array(" ", " "), $string);
return nl2br($string);
}
/*
* 功能:格式化文本輸出
* 參數 $text 為需格式化的常值內容
*/
function formatcontent($text){
$trans = get_html_translation_table(HTML_SPECIALCHARS);
$trans = array_flip($trans);
$text = strtr($text, $trans);
//$text = str_replace("\n", "<br>", $text);
//$text = str_replace(" ", " ", $text);
return $text;
}
/*
* 將位元組轉換成Kb或者Mb
* 參數 $num為位元組大小
*/
function bitsize($num){
if(!preg_match("/^[0-9]+$/", $num)) return 0;
return $num > 1024 ? ($num/1024 > 1024 ? round($num/1024/1024, 2)." Mb" : round($num/1024, 2)." Kb") : $num." 位元組";
}
/*
* 防注入處理(為變數加入斜杠)函數
* 參數 $array 為防注入變數數組
*/
function add_s(&$array){
foreach($array as $key=>$value){
if(!is_array($value)){
$array[$key]=addslashes($value);
}else{
$this->add_s($array[$key]);
}
}
}
/*
* 轉換HTML特殊字元(表單提交的時候使用,防止惡意JS代碼)
* 參數 $array 為需轉換的字串或者數組
*/
function specialhtml(&$array){
if(is_array($array)){//數組處理
foreach($array as $key=>$value){
if(!is_array($value)){
$array[$key]=htmlspecialchars($value);
}else{
$this->specialhtml($array[$key]);
}
}
}else{
$array = htmlspecialchars($array);
}
}
/*
* 可以避免亂碼的截取漢字
* 參數 $str 為字串,$start 為開始字元,$len 結束字元
* 返回截取後的字元
*/
function msubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
/*
* 功能:綜合提示JS代碼輸出
* 參數 $msg 為提示資訊
* $direct 為提示類型 0為提示(預設)1為提示重新整理返回 2為提示返回
* 輸出提示代碼並結束程式
*/
function alert_msg($msg, $direct = "0"){
switch($direct){
case '0'://提示
$script = "";
case '1'://提示重新整理返回
$script = "location.href=\"".$_SERVER["HTTP_REFERER"]."\";";
break;
case '2'://提示返回
$script = "history.back();";
break;
default://提示轉向指定頁面
$script = "location.href=\"".$direct."\";";
}
echo "<script language='javascript'>window.alert('".$msg."');".$script."</script>";
exit;
}
/*
* 功能:取得給定日期所在周的開始日期和結束日期
* 參數:$gdate 日期,預設為當天,格式:YYYY-MM-DD
* $first 一周以星期一還是星期天開始,0為星期天,1為星期一
* 返回:數組array("開始日期", "結束日期");
*/
function aweek($gdate = "", $first = 0){
if(!$gdate) $gdate = date("Y-m-d");
$w = date("w", strtotime($gdate));//取得一周的第幾天,星期天開始0-6
$dn = $w ? $w - $first : 6;//要減去的天數
$st = date("Y-m-d", strtotime("$gdate -".$dn." days"));
$en = date("Y-m-d", strtotime("$st +6 days"));
return array($st, $en);//返回開始和結束日期
}
/*
* 功能:檢測頁面是否合法串連過來
* 如果為非法,就轉向到登陸視窗
*/
function checkurl(){
//如果直接從瀏覽器串連到頁面,就串連到登陸視窗
//echo "referer:".$_SERVER['HTTP_REFERER'];
if(!isset($_SERVER['HTTP_REFERER'])) {
header("location: index.php");
exit;
}
$urlar = parse_url($_SERVER['HTTP_REFERER']);
//如果頁面的網域名稱不是伺服器網域名稱,就串連到登陸視窗
if($_SERVER["HTTP_HOST"] != $urlar["host"]) {
header("location: index.php");
exit;
}
}
/*
* 讀取檔案內容
* 參數 $file 為檔案名稱及完整路徑
* 返迴文件內容
*/
function readfiles($file){
$tdata = "";
$fp = fopen($file, "r");
if(filesize($file) <= 0) return;
while($data = fread($fp, filesize($file))){
$tdata .= $data;
}
fclose($fp);
return $tdata;
}
}
?>