Home  >  Article  >  Backend Development  >  Collection of commonly used functions in PHP development process_PHP tutorial

Collection of commonly used functions in PHP development process_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:50849browse

1. Print array function

Copy code The code is as follows:

function _print($array)
{
echo ("
"); <br>print_r($array); <br>echo ("
");
}

2. Intercept words String
Copy code The code is as follows:

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. Load file
Copy code The code is as follows:

loadFile
function loadFile($filepath)
{
$filecontent = "";
$ fptr = fopen($filepath,"r");
if ($fptr)
{
while ($content = fgets($fptr,4096))
{
$filecontent . = $content;
}
fclose($fptr);
}
return $filecontent;
}

4. Download file
downloadFile
Copy code The code is as follows:

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. Array sorting
Copy code The code is as follows:

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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320925.htmlTechArticle1. Print array function copy code The code is as follows: function _print($array) { echo ("pre"); print_r($array); echo ("/pre"); } 2. Intercept the string and copy the code. The code is as follows: func_chgtitle fun...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn