Summary of common PHP skills

Source: Internet
Author: User
Tags php regular expression
: This article describes common PHP skills. For more information about PHP tutorials, see.
Php file read function // file read function PHP_Read ($ file_name) {$ fd = fopen ($ file_name, r); while ($ bufline = fgets ($ fd, 4096 )) {$ buf. = $ bufline;} fclose ($ fd); return $ buf ;}?> File write 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 phptohtm ($ filefrom, $ fileto, $ u2u = 1) {if ($ u2u = 1) {$ data = PHP_Read ($ filefrom);} else {$ data = $ filefrom;} PHP_Write ($ fileto, $ data); return true;}?> Specify the number of condition information to retrieve the 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: 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 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 function // paging 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) ;}?> PHP paging function usage instructions: PHP image file upload function // image file upload function upload_img ($ UploadFile, $ UploadFile_name, $ UploadFile_size, $ UploadPath, $ max_size = 64) {// $ TimeLimit = 60; // Set the time-out limit. the default time is 30 seconds. if it is set to 0, it 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 settings} below are some tips: how does PHP determine the legality of IP addresses if (! Strcmp (long2ip (sprintf ("% u", ip2long ($ ip), $ ip) echo "is ipn "; -- eregi ("^ [_. 0-9a-zA-Z-] + @ ([0-9a-zA-Z] [0-9a-zA-Z _-] + .) + [a-zA-Z] $ ", $ email); an example of checking whether the ip address and mask are valid $ ip = '100. 168.0.84 '; $ mask = '2017. 255.255.0 '; $ network = '2017. 168.0 '; $ ip = ip2long ($ ip); $ mask = ip2long ($ mask); $ network = ip2long ($ network); if ($ ip & $ mask) ==$ network) echo "valid ip and maskn";?> -- How to set the header for PHP file download header output ("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 outputs the ftp download method using the header, an example of resumable upload is also supported: 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 Technical development knowledge base/cn_web.rmvb "); PHP regular expression matching (" ^ [". chr (0xa1 ). "-". chr (0xff ). "] + $", $ str); replace the hyperlink 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 above is a summary of common PHP skills, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.