PHP Common function library and some practical tips _php tips
Last Update:2017-01-19
Source: Internet
Author: User
file-Reading functions
FILE-Reading functions
function Php_read ($file _name) {
$FD =fopen ($file _name,r);
while ($bufline =fgets ($FD, 4096)) {
$buf. = $bufline;
}
Fclose ($FD);
return $buf;
}
?>
File Write function 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
Static page-generation 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;
}
?>
Specify conditional information quantity retrieval function
Specify conditional information Quantity retrieval function
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";
}
?>
The validity of directory name 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 ("Not conforming to the specification!");
}
$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 ("This edition has not yet chosen to release news!");
}
$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);
}
?>
instructions on how to use a paging function /*
# # Retrieve paging function # # # # # # #
Int $HALT-whether (1/0) displays page numbers when the results are only 1 pages
Int $LRLIST-(page number shows page numbers-1)/2
Int $ECHOCNT-Number of records displayed per page when retrieving
Int $paper-pages, pre-extraction: $paper = $HTTP _get_vars[paper];
Varchar $table-Data table name, pre-attached value: $table = "db.table";
Varchar $where-Search condition, preload value: $where = "where field= ' value '";
Varchar $enwhere-Commits the original $where two times Base64_encode () encoded and then submitted in get
Varchar $SPLITSTR-page-number bar output string, execution function followed by the Echo $splitstr at the appropriate location;
You need to get a variable before a function call-
$paper = $HTTP _get_vars[paper];
$sumcnt = $HTTP _get_vars[sumcnt];
$enwhere = $HTTP _get_vars[enwhere];
Return (Varchar $where)-Retrieving criteria for retrieving statements after paging
Note: This function calls for an error-handling function nerror ($error);
*/
Picture File Upload function
Picture File Upload function
function upload_img ($UploadFile, $UploadFile _name, $UploadFile _size, $UploadPath, $max _size=64) {
$TimeLimit = 60; Set timeout limit time defaults to 30 seconds set to 0 when 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 ("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 settings
}
}
Here are some tips:
How to judge the legality of IP address if (!strcmp (Long2ip (sprintf ("%u", Ip2long ($IP)), $IP)) echo "is IPN";
----
the regular judgment of the email Eregi ("^[_.0-9a-za-z-]+@" ([0-9a-za-z][0-9a-za-z_-]+.) +[a-za-z]$ ", $email);
Examples of whether IP addresses and mask are legitimate
$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 MASKN";
?>
----
file Download header 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 '
FTP Download mode with header output and support for breakpoint continuation
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");
regular Match Chinese
Ereg ("^[". Chr (0XA1). " -". Chr (0xFF)."] +$ ", $STR);
Bulk Replace hyperlinks in text
function Urlparse ($str = ' ") {
if (' = = $str ') return $str;
$types = Array ("http", "FTP", "https");
$replace = <<
'. Htmlentities (' \1 '). Htmlentities (' \2 ').
eophp;
$ret = $str;
while (the list (, $type) = each ($types)) {
$ret = Preg_replace ("| $type://) ([^\s]*) |ie ", $replace, $ret);
}
return $ret;
}