欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP开发过程中常用函数收藏

程序员文章站 2023-10-31 23:11:58
1.打印数组函数 复制代码 代码如下: function _print($array) { echo ("
"); print_r($array); e...
1.打印数组函数
复制代码 代码如下:

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.数组排序
复制代码 代码如下:

/**
* @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;
}

来源: