Common PHP function libraries and some practical tips

Source: Internet
Author: User
Tags ranges

File Read Function
// File Read Function
Function php_read ($ file_name ){
$ FD = fopen ($ file_name, R );
While ($ bufline = fgets ($ FD, 4096 )){
$ Buf. = $ bufline;
}
Fclose ($ FD );
Return $ Buf;
}
?>
File Writing Function
// File Writing Function
Function php_write ($ file_name, $ data, $ method = "W "){
$ Filenum = @ fopen ($ file_name, $ method );
Flock ($ filenum, lock_ex );
$ File_data = fwrite ($ filenum, $ data );
Fclose ($ filenum );
Return $ file_data;
}
?>
Static Page generation function
// Static page generation function
Function phptohtm ($ filefrom, $ fileto, $ u2u = 1 ){
If ($ u2u = 1 ){
$ DATA = php_read ($ filefrom );
} Else {
$ DATA = $ filefrom;
}
Php_write ($ fileto, $ data );
Return true;
}
?>
Number of specified conditions
// Query the number of specified conditions
Function rec_exist ($ table, $ where ){
$ Query = "select count (*) as num from $ table". $ where;
$ Result = mysql_query ($ query) or die (nerror (1 ));
$ Rowcount = mysql_fetch_array ($ result );
$ Num = $ rowcount ["num"];
If ($ num = 0 ){
Return false;
}
Return $ num;
}
?>
Directory deletion function
// Directory deletion function
Function del_dir ($ directory ){
$ Mydir = Dir ($ directory );
While ($ file = $ mydir-> Read ()){
If (is_dir ("$ directory/$ File") and ($ file! = ".") And ($ file! = "..")){
Del_dir ("$ directory/$ File ");
} Else {
If ($ file! = ".") And ($ file! = "..")){
Unlink ("$ directory/$ File ");
// Echo "unlink $ directory/$ file OK ";
}
}
}
$ Mydir-> close ();
Rmdir ($ directory );
// Echo "rmdir $ directory OK ";
}
?>
Directory Name validity check function
// Check the validity of the Directory Name
Function Isen ($ Str ){
$ Ret = "";
For ($ I = 0; $ I $ P = ord (substr ($ STR, $ I, 1 ));
If ($ P <48 & $ p! = 45 & $ p! = 46) | ($ P> 57 & $ P <65) | ($ P> 90 & $ P <97 & $ p! = 95) ||$ P> 122 ){
Nerror ("non-compliant! ");
}
$ Ret. = CHR ($ P );
}
Return $ ret;
}
?>
Paging Function
// Paging Function
Function splitlist ($ halt, $ lrlist, $ echocnt, $ paper, $ table, $ where, $ page_id, $ userid ){
Global $ splitstr, $ sumcnt;
If ($ paper = "" | $ sumcnt = ""){
$ Query = "select count (*) as num from $ table $ where ";
$ Result = mysql_query ($ query );
$ ROW = mysql_fetch_array ($ result );
$ Sumcnt = $ row ["num"];
If ($ sumcnt = 0 ){
Nerror ("no news has been selected for this version! ");
}
$ Paper = 1;
}
$ Sumpaper = ($ sumcnt-$ sumcnt % $ echocnt)/$ echocnt;
If ($ sumcnt % $ echocnt )! = 0) $ sumpaper + = 1;
If ($ sumpaper = 1 & $ halt = 0) Return ($ where );
$ Enwhere = base64_encode (base64_encode ($ where ));
If ($ lrlist * 2 + 1) <$ sumpaper ){
If ($ paper-$ lrlist) <2 ){
$ Tract = 1;
$ Sub = $ lrlist * 2 + 1;
} Else if ($ paper + $ lrlist) >=$ sumpaper ){
$ Tract = $ sumpaper-($ lrlist * 2 );
$ Sub = $ sumpaper;
} Else {
$ Tract = $ paper-$ lrlist;
$ Sub = $ paper + $ lrlist;
}
} Else {
$ Tract = 1;
$ Sub = $ sumpaper;
}
$ Uppaper = $ paper-1;
$ Downpaper = $ paper + 1;
$ Startcnt = ($ paper-1) * $ echocnt;
$ Where. = "Limit $ {startcnt}, $ {echocnt }";
If ($ tract> 1) {$ splitstr = "[<";}
Else $ splitstr = "[<";
For ($ I = $ tract; $ I <= $ sub; $ I ++ ){
If ($ I! = $ Paper) $ splitstr. = "". $ I ."";
Else $ splitstr. = "". $ I ."";
}
If ($ sub! = $ Sumpaper) $ splitstr. = "> ]";
Else $ splitstr. = "> ]";
Return ($ where );
}
?>
Use of paging Functions
/*
#### Retrieve paging functions ####
Int $ halt-whether the page number is displayed (1/0) when the search result is divided into only one page
Int $ lrlist-(number of displayed page numbers-1)/2
Int $ echocnt-number of records displayed per page during retrieval
Int $ paper-page number, pre-extracted: $ paper = $ http_get_vars [Paper];
Varchar $ table-data table name, with a pre-attached value: $ table = "DB. Table ";
Varchar $ where-search condition, with a value prefixed: $ where = "where field = 'value '";
Varchar $ enwhere-encode the original $ where twice and then submit it in get Mode
Varchar $ splitstr-the output string of the page number. After executing the function, execute echo $ splitstr at the corresponding position;
Get the variable before calling the function-
$ Paper = $ http_get_vars [Paper];
$ Sumcnt = $ http_get_vars [sumcnt];
$ Enwhere = $ http_get_vars [enwhere];
Return (varchar $ where)-Search Condition of the paging query statement
Note: This function calls the error handling function nerror ($ error );
*/
Image File upload function
// Image file upload function
Function upload_img ($ uploadfile, $ uploadfile_name, $ uploadfile_size, $ uploadpath, $ max_size = 64 ){
// $ Timelimit = 60; // set the time-out period. The default time is 30 seconds. If the value is set to 0, the time-out period is not limited.
// Set_time_limit ($ timelimit );
If ($ uploadfile! = "NONE") & ($ uploadfile! = "")){
$ Filename = $ uploadpath. $ uploadfile_name;
If ($ uploadfile_size <1024 ){
$ Filesize = "(string) $ uploadfile_size". "Byte ";
} Elseif ($ uploadfile_size <(1024 * $ max_size )){
$ Filesize = number_format (double) ($ uploadfile_size/1024), 1). "kb ";
} Else {
Nerror ("the file size exceeds the limit! ");
}
//{
// $ Filesize = "number_format (double) ($ uploadfile_size"/(1024*1024), 1). "MB ";
//}
If (! File_exists ($ filename )){
If (copy ($ uploadfile, $ filename )){
Return "$ uploadfile_name ($ filesize )";
} Else {
Nerror ("File $ uploadfile_name Upload Failed! ");
}
Unlink ($ uploadfile );
} Else {
Nerror ("File $ uploadfile_name already exists! ");
}
// Set_time_limit (30); // restore the default timeout value
}
}
TIPS:
How to determine IP address Legality
If (! Strcmp (long2ip (sprintf ("% u", ip2long ($ IP), $ IP) echo "is IPN ";
----
Email Regular Expression judgment
Eregi ("^ [_. 0-9a-za-z-] + @ ([0-9a-za-z] [0-9a-za-z _-] +.) + [A-Za-Z] $", $ email );
Example of checking whether IP addresses and masks are valid
$ IP = '1970. 168.0.84 ';
$ Mask = '2017. 255.255.0 ';
$ Network = '1970. 100 ';
$ IP = ip2long ($ IP );
$ Mask = ip2long ($ mask );
$ Network = ip2long ($ network );
If ($ IP & $ mask) = $ Network) echo "valid IP and maskn ";
?>
----
How to Set File Download header output
Header ("Content-Type: Application/X-download ");
Header ("content-Disposition: attachment; filename = $ file_download_name ;");
Header ("Accept-ranges: bytes ");
Header ("Content-Length: $ download_size ");
Echo 'xxx'
Output FTP download mode with header and support resumable Data Transfer
Example:
Header ('pragma: Public ');
Header ('cache-control: Private ');
Header ('cache-control: No-cache, must-revalidate ');
Header ('Accept-ranges: bytes ');
Header ('Connection: close ');
Header ("Content-Type: Audio/MPEG ");
Header ("Location: ftp: // download: 1bk3l4s3k9s2@232.2.22.22/2222/WEB technology development knowledge base/cn_web.rmvb ");
Regular Expression matching (Chinese)
Ereg ("^ [". CHR (0xa1). "-". CHR (0xff). "] + $", $ Str );
Replace hyperlinks in text in batches
Function urlparse ($ STR = ''){
If (''= $ Str) return $ STR;
$ Types = array ("HTTP", "ftp", "HTTPS ");
$ Replace = <
''.Html entities ('\ 1'hangzhou.html entities (' \ 2 ').''
Eophp;
$ Ret = $ STR;
While (List (, $ type) = each ($ types )){
$ Ret = preg_replace ("| ($ type: //) ([^ \ s] *) | ie", $ replace, $ RET );
}
Return $ ret;
}

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.