PHP Functions Collection

Source: Internet
Author: User
This will collect some of the PHP functions I used in my work. Have their own written, there will be online collection. This article is constantly updated in
1. Print array functions

function _print ($array)
{
Echo ("

" );    
Print_r ($array);
Echo
" );
}

2. Intercept strings

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

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
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

Code

/* *
* @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 the use Array_multisort () function.
*
* Syssortarray ($Array, "Key1", "Sort_asc", "Sort_retular", "Key2" ...)
* @author Chunsheng Wang
* @param array $ArrayData the array to sort.
* @param string $KeyName 1 The first item to sort by.
* @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;
}

  • 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.