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

yipeiwu_com5年前PHP代码库
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;
}

来源:http://www.cnblogs.com/xiaosuo/archive/2009/12/14/1594455.html

相关文章

php debug 安装技巧

本打算配置ZendDebugger 进行调试,配置了老长时间没配置成功,在phpinfo看不到ZendDebugger生效,经查发现是php5.3的问题,必须先卸载,本文介绍使用xdeb...

整理php防注入和XSS攻击通用过滤

对网站发动XSS攻击的方式有很多种,仅仅使用php的一些内置过滤函数是对付不了的,即使你将filter_var,mysql_real_escape_string,htmlentities...

php为字符串前后添加指定数量字符的方法

本文实例讲述了php为字符串前后添加指定数量字符的方法。分享给大家供大家参考。具体分析如下: 这段php代码定义了两个函数 str_prefix和str_suffix,分别用来给字符串前...

如何用PHP做到页面注册审核

如何用PHP做到页面注册审核

用户注册后就有该条用户记录,你对用户表设一个“审核状态”字段,默认值设为“未审核”,然后你写几句审核代码做成一个功能,按照你们的意愿若审核通过你把审核状态改为“已审核”就行了。用户想进行...

PHP识别二维码的方法(php-zbarcode安装与使用)

本文实例讲述了PHP识别二维码的方法。分享给大家供大家参考,具体如下: 说明:扩展需要依赖ImageMagick和zbar,安装前先安装这两个软件 1.安装ImageMagick(htt...