PHP開發(fā)過程中常用函數(shù)收藏
更新時(shí)間:2009年12月14日 23:48:15 作者:
此中將收藏我在工作中用到的一些PHP函數(shù)。有自己寫的,也會有網(wǎng)上收集的。此文不斷更新中
1.打印數(shù)組函數(shù)
function _print($array)
{
echo ("<pre>");
print_r($array);
echo ("</pre>");
}
2.截取字串
func_chgtitle
function func_chgtitle($str,$len)
{
if(strlen($str)>$len)
{
$tmpstr = "";
$strlen = $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."";
}
else
{
return $str;
}
}
3.加載文件
loadFile
function loadFile($filepath)
{
$filecontent = "";
$fptr = fopen($filepath,"r");
if ($fptr)
{
while ($content = fgets($fptr,4096))
{
$filecontent .= $content;
}
fclose($fptr);
}
return $filecontent;
}
4.下載文件
downloadFile
function downloadFile($path,$fileInfo)
{
$target_file = $path.$fileInfo['fileid'];
$file_content = loadFile($target_file);
header("Content-Disposition: attachment; filename=".$fileInfo['filename']);
header("Content-type: ".$fileInfo['filetype']);
header("Content-Length: ".$fileInfo['filesize']);
echo $file_content;
}
5.數(shù)組排序
/**
* @package BugFree
* @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
*
*
* Sort an two-dimension array by some level two items use array_multisort() function.
*
* sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
* @author Chunsheng Wang <wwccss@263.net>
* @param array $ArrayData the array to sort.
* @param string $KeyName1 the first item to sort by.
* @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC")
* @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
* @return array sorted array.
*/
function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
{
if(!is_array($ArrayData))
{
return $ArrayData;
}
// Get args number.
$ArgCount = func_num_args();
// Get keys to sort by and put them to SortRule array.
for($I = 1;$I < $ArgCount;$I ++)
{
$Arg = func_get_arg($I);
if(!eregi("SORT",$Arg))
{
$KeyNameList[] = $Arg;
$SortRule[] = '$'.$Arg;
}
else
{
$SortRule[] = $Arg;
}
}
// Get the values according to the keys and put them to array.
foreach($ArrayData AS $Key => $Info)
{
foreach($KeyNameList AS $KeyName)
{
${$KeyName}[$Key] = $Info[$KeyName];
}
}
// Create the eval string and eval it.
if(count($ArrayData)>0)
{
$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval ($EvalString);
}
return $ArrayData;
}
來源:http://www.cnblogs.com/xiaosuo/archive/2009/12/14/1594455.html
復(fù)制代碼 代碼如下:
function _print($array)
{
echo ("<pre>");
print_r($array);
echo ("</pre>");
}
2.截取字串
復(fù)制代碼 代碼如下:
func_chgtitle
function func_chgtitle($str,$len)
{
if(strlen($str)>$len)
{
$tmpstr = "";
$strlen = $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."";
}
else
{
return $str;
}
}
3.加載文件
復(fù)制代碼 代碼如下:
loadFile
function loadFile($filepath)
{
$filecontent = "";
$fptr = fopen($filepath,"r");
if ($fptr)
{
while ($content = fgets($fptr,4096))
{
$filecontent .= $content;
}
fclose($fptr);
}
return $filecontent;
}
4.下載文件
downloadFile
復(fù)制代碼 代碼如下:
function downloadFile($path,$fileInfo)
{
$target_file = $path.$fileInfo['fileid'];
$file_content = loadFile($target_file);
header("Content-Disposition: attachment; filename=".$fileInfo['filename']);
header("Content-type: ".$fileInfo['filetype']);
header("Content-Length: ".$fileInfo['filesize']);
echo $file_content;
}
5.數(shù)組排序
復(fù)制代碼 代碼如下:
/**
* @package BugFree
* @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
*
*
* Sort an two-dimension array by some level two items use array_multisort() function.
*
* sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
* @author Chunsheng Wang <wwccss@263.net>
* @param array $ArrayData the array to sort.
* @param string $KeyName1 the first item to sort by.
* @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC")
* @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
* @return array sorted array.
*/
function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
{
if(!is_array($ArrayData))
{
return $ArrayData;
}
// Get args number.
$ArgCount = func_num_args();
// Get keys to sort by and put them to SortRule array.
for($I = 1;$I < $ArgCount;$I ++)
{
$Arg = func_get_arg($I);
if(!eregi("SORT",$Arg))
{
$KeyNameList[] = $Arg;
$SortRule[] = '$'.$Arg;
}
else
{
$SortRule[] = $Arg;
}
}
// Get the values according to the keys and put them to array.
foreach($ArrayData AS $Key => $Info)
{
foreach($KeyNameList AS $KeyName)
{
${$KeyName}[$Key] = $Info[$KeyName];
}
}
// Create the eval string and eval it.
if(count($ArrayData)>0)
{
$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval ($EvalString);
}
return $ArrayData;
}
來源:http://www.cnblogs.com/xiaosuo/archive/2009/12/14/1594455.html
您可能感興趣的文章:
- PHP執(zhí)行l(wèi)inux命令常用函數(shù)匯總
- PHP中的一些常用函數(shù)收集
- 非常實(shí)用的PHP常用函數(shù)匯總
- PHP 正則表達(dá)式常用函數(shù)
- ThinkPHP字符串函數(shù)及常用函數(shù)匯總
- PHP常用函數(shù)和常見疑難問題解答
- 基于PHP中的常用函數(shù)回顧
- 基于php常用函數(shù)總結(jié)(數(shù)組,字符串,時(shí)間,文件操作)
- PHP5常用函數(shù)列表(分享)
- 基于PHP常用函數(shù)的用法詳解
- PHP 查找字符串常用函數(shù)介紹
- PHP中的MYSQL常用函數(shù)(php下操作數(shù)據(jù)庫必備)
- 收藏的PHP常用函數(shù) 推薦收藏保存
- PHP 正則表達(dá)式常用函數(shù)使用小結(jié)
- PHP 常用函數(shù)庫和一些實(shí)用小技巧
- PHP常用函數(shù)小技巧
- 繼續(xù)收藏一些PHP常用函數(shù)
- PHP常用函數(shù)總結(jié)(180多個(gè))
相關(guān)文章
php基于ob_start(ob_gzhandler)實(shí)現(xiàn)網(wǎng)頁壓縮功能的方法
這篇文章主要介紹了php基于ob_start('ob_gzhandler')實(shí)現(xiàn)網(wǎng)頁壓縮功能的方法,涉及php中ob_gzip、ob_start等函數(shù)操作緩沖區(qū)及內(nèi)容壓縮相關(guān)技巧,需要的朋友可以參考下2017-02-02PHP實(shí)現(xiàn)截取中文字符串不出現(xiàn)?號的解決方法
這篇文章主要介紹了PHP實(shí)現(xiàn)截取中文字符串不出現(xiàn)?號的解決方法,涉及php字符串遍歷及編碼轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2016-12-12