Summary of common PHP skills (with function code) _ PHP Tutorial

Source: Internet
Author: User
Summary of common PHP skills (with function code ). Code for copying a php file read function: functionPHP_Read ($ file_name) {$ fdfopen ($ file_name, r); while ($ buflinefgets ($ fd, 4096 )) {$ buf. $ bufline;} f php file read function

The code is as follows:


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

The code is as follows:


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

The code is as follows:


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

The code is as follows:


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

The code is as follows:


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

The code is as follows:


// 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 ("not compliant with specifications !");
}
$ Ret. = chr ($ p );
}
Return $ ret;
}
?>


PHP paging functions

The code is as follows:


// 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 in PHP

The code is as follows:


/*
#### 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 pre-attached value: $ 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 );
*/
PHP 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 ("the file $ UploadFile_name already exists !");
}
// Set_time_limit (30); // restore the default timeout value
}
}


Tips:

How PHP judges 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 php 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'
PHP uses the header to output the ftp download method and supports 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 ");
PHP 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 = <
2.16.htmlentities('1100000000.html entities ('2 ′)."
EOPHP;
$ Ret = $ str;
While (list (, $ type) = each ($ types )){
$ Ret = preg_replace ("| ($ type: //) ([^ s] *) | ie", $ replace, $ ret );
}
Return $ ret;
}

The handler code is as follows: // file read function PHP_Read ($ file_name) {$ fd = fopen ($ file_name, r); while ($ bufline = fgets ($ fd, 4096 )) {$ buf. = $ bufline;} f...

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.