_php techniques of common function collection in PHP development process

Source: Internet
Author: User
1. Print Array function
Copy Code code as follows:

function _print ($array)
{
Echo ("<pre>");
Print_r ($array);
Echo ("</pre>");
}

2. Intercept string
Copy Code code 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. Loading files
Copy Code code 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 the file
DownloadFile
Copy Code code 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 Ordering
Copy Code code 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 the 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 $KeyName 1 The ' the ' the ' the '.
* @param string $SortOrder 1 The order to sort by ("SORT_ASC" |) Sort_desc ")
* @param string $SortType 1 the Sort type ("Sort_regular" |) Sort_numeric "|" Sort_string ")
* @return Array sorted array.
*/
function Syssortarray ($ArrayData, $KeyName 1, $SortOrder 1 = "Sort_asc", $SortType 1 = "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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.