Common functions during PHP Development

Source: Internet
Author: User
Favorite common functions during PHP development. 1. code for copying an array function: function_print ($ array) {echo (pre); print_r ($ array); echo (pre) ;}2. the code for copying a string is as follows: func_chgtitlefun 1. print array functions

The code is as follows:


Function _ print ($ array)
{
Echo ("

"); 
print_r($array);
echo ("
");
}


2. truncate a string

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 files

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 an object
DownloadFile

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

The code is as follows:


/**
* @ Package BugFree
* @ Version $ Id: FunctionsMain. inc. php, v 1.32 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.
* @ 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

The compile code is as follows: function _ print ($ array) {echo ("pre"); print_r ($ array); echo ("/pre");} 2. the code for intercepting the string is as follows: func_chgtitle fun...

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.