PHP Common Skills Summary, PHP summary _php tutorial

Source: Internet
Author: User
Tags ranges rowcount knowledge base

PHP Common Skills Summary, PHP summary


This article for you to share a number of common PHP skills, for your reference, specific content as follows

1. php file read function

The file reads function php_read ($file _name) {$fd =fopen ($file _name,r), while ($bufline =fgets ($FD, 4096)) {$buf. = $bufline;} Fclose ($FD); return $buf;}? >

2. File Write function

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

Static page-generated function function phptohtm ($filefrom, $fileto, $u 2u=1) {if ($u 2u==1) {$data =php_read ($filefrom);} else{$data = $filefrom;} Php_write ($fileto, $data); return true;}? >

4, specify the number of conditional information retrieval function

Specify the number of conditional information 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 Delete 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 legality detection function

Directory name legality detection 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 ("does not conform to specification!");} $ret. =CHR ($p);} return $ret;}? >

7. PHP Paging function

Paging functions 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 = My Sql_fetch_array ($result); $sumcnt = $row ["num"];if ($sumcnt ==0) {nerror ("No news has been selected in the release!");} $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 *); $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 usage Instructions for paging functions
8, php image file Upload function

Picture File Upload function upload_img ($UploadFile, $UploadFile _name, $UploadFile _size, $UploadPath, $max _size=64) {//$ timelimit=60; Set the time-out limit to 30 seconds when the default is set to 0 when//set_time_limit ($TimeLimit); if ($UploadFile! = "None") && ($UploadFile! = "")) {$ Filename= $UploadPath. $UploadFile _name;if ($UploadFile _size <1024) {$FileSize = "(string) $UploadFile _size". "Byte";} ElseIf ($UploadFile _size < (1024x768 * $max _size)) {$FileSize =number_format ((double) ($UploadFile _size/1024), 1). "KB";} Else{nerror ("File exceeds limit size!");} {//$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 default Timeout setting}}

Here are some tips:
1. How PHP determines IP address legality

if (!strcmp (Long2ip (sprintf ("%u", Ip2long ($IP))), $IP)) echo "is IPN";--email's regular judgment Eregi ("^[_.0-9a-za-z-]+@ ([ 0-9a-za-z][0-9a-za-z_-]+.) +[a-za-z]$ ", $email); an example of whether the IP address and mask are valid  $ip = ' 192.168.0.84′; $mask = ' 255.255.255.0′; $network = ' 192.168.0′; $ip = Ip2long ($IP), $mask = Ip2long ($mask), $network = Ip2long ($network), if (($ip & $mask) = = $network) echo "Valid IP and MAS KN ";? >--

2, php file download head output how to set

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 with header output FTP download mode, and support the continuation of the breakpoint
An 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 match Chinese

Ereg ("^[". Chr (0XA1). " -". Chr (0xff)." +$ ", $STR);

5, bulk replacement text inside the hyperlink

function Urlparse ($str = ") {if (" = = $str) return $str; $types = Array ("http", "FTP", "https"); $replace = << ". htmlent Ities (' 1′). Htmlentities (' 2′). " eophp; $ret = $str; while (list (, $type) = each ($types)) {$ret = Preg_replace ("| ( $type://) ([^s]*) |ie ", $replace, $ret);} return $ret;}

The above is the whole content of this article, very common PHP skills to everyone, I hope you like.

The following is for everyone to share the topic content, the content is very comprehensive, see the "PHP Common skills summary."

Articles you may be interested in:

    • 8 tips commonly used in PHP development
    • PHP Common Functions Tips
    • PHP Common function library and some practical tips
    • PHP Delimiter usage Tips
    • PHP include_path Setup Tips sharing
    • PHP Array Operations summary PHP array usage tips
    • Summary of common skills in PHP (with function code)
    • PHP timestamp Strtotime () using methods and tricks
    • PHP Web site development commonly used in 8 tips

http://www.bkjia.com/PHPjc/1106124.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106124.html techarticle PHP Common Skills Summary, PHP Summary This article for you to share a number of common PHP skills, for your reference, the specific content as follows 1, PHP file read function//file read function Php_re ...

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