Summary of common PHP skills

Source: Internet
Author: User
Tags php regular expression
This article provides a detailed summary of common PHP skills. interested friends can refer to this article to share multiple common php skills for your reference. the specific content is as follows:

1. php file read function

// Function PHP_Read ($ file_name) {$ fd = fopen ($ file_name, r); while ($ bufline = fgets ($ fd, 4096) {$ buf. = $ bufline;} fclose ($ fd); return $ buf ;}?>

2. 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 ;}?>

3. 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 ;}?>

4. specify the number of condition information to retrieve.

// 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 ;}?>

5. Directory deletion function

// 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";}?>

6. directory name validity check function

// 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;}?>

7. PHP paging functions

// 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) ;}?>

Use of paging functions in PHP
8. PHP image file upload function

// Function upload_img ($ UploadFile, $ UploadFile_name, $ UploadFile_size, $ UploadPath, $ max_size = 64) {// $ TimeLimit = 60; // Set the timeout 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 value }}

Tips:
1. how does PHP determine IP address legality?

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";?> --

2. how to set the 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'

3. 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 ");

4. PHP regular expression matching

ereg(“^[".chr(0xa1)."-".chr(0xff)."]+$”, $str);

5. replace hyperlinks in the text in batches

function urlParse($str = ”){if (” == $str) return $str;$types = array(“http”, “ftp”, “https”);$replace = <<”.htmlentities('1′).htmlentities('2′).”EOPHP;$ret = $str;while(list(,$type) = each($types)){$ret = preg_replace(“|($type://)([^s]*)|ie “, $replace, $ret);}return $ret;}

The above is all of the content in this article. I hope you will like it if you are familiar with php skills.

The following is the topic content shared for you. the content is comprehensive and can be viewed.Summary of common php skills.

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.