Add some common PHP functions to favorites

Source: Internet
Author: User
Tags date1 ereg flock php excel gtar
It is convenient to make php programs later and you can use them directly. In fact, some good functions are available in some mature cms systems, such as dedecms, Empire cms, and phpcms, both gb2312 and utf8 are supported. It is convenient to make php programs later and you can use them directly. In fact, some good functions are available in some mature cms systems, such as dedecms, Empire cms, and phpcms, both gb2312 and utf8 are supported.

 
 
 = 0 & $ timeArr [0] <= 23) & ($ timeArr [1]> = 0 & $ timeArr [1] <= 59) & ($ timeArr [2]> = 0 & $ timeArr [2] <= 59) return true; else return false ;} function DateDiff ($ date1, $ date2, $ unit = "") {// Time comparison function, returns the seconds, minutes, hours, or days of difference between two dates switch ($ unit) {case's ': $ pidend = 1; break; case' I ': $ pidend = 60; break; case 'H': $ pidend = 3600; break; case 'D': $ pidend = 86400; break; default: $ pide Nd = 86400 ;}$ time1 = strtotime ($ date1); $ time2 = strtotime ($ date2); if ($ time1 & $ time2) return (float) ($ time1-$ time2)/$ pidend; return false ;}?>


PHP redirection

Method 1: header ("Location: index. php"); Method 2: echo"
 
  
Window. location = "$ PHP_SELF ";
 "; Method 3: echo"
 ";?>

Obtain the visitor's browser

 

Obtain the visitor's operating system

 

File format

  'image/gif', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpe' => 'image/jpeg', 'bmp' => 'image/bmp', 'png' => 'image/png', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'pict' => 'image/x-pict', 'pic' => 'image/x-pict', 'pct' => 'image/x-pict', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'psd' => 'image/x-photoshop', 'swf' => 'application/x-shockwave-flash', 'js' => 'application/x-javascrīpt', 'pdf' => 'application/pdf', 'ps' => 'application/postscrīpt', 'eps' => 'application/postscrīpt', 'ai' => 'application/postscrīpt', 'wmf' => 'application/x-msmetafile', 'css' => 'text/css', 'htm' => 'text/html', 'html' => 'text/html', 'txt' => 'text/plain', 'xml' => 'text/xml', 'wml' => 'text/wml', 'wbmp' => 'image/vnd.wap.wbmp', 'mid' => 'audio/midi', 'wav' => 'audio/wav', 'mp3' => 'audio/mpeg', 'mp2' => 'audio/mpeg', 'avi' => 'video/x-msvideo', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'lha' => 'application/x-lha', 'lzh' => 'application/x-lha', 'z' => 'application/x-compress', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', 'gzip' => 'application/x-gzip', 'tgz' => 'application/x-gzip', 'tar' => 'application/x-tar', 'bz2' => 'application/bzip2', 'zip' => 'application/zip', 'arj' => 'application/x-arj', 'rar' => 'application/x-rar-compressed', 'hqx' => 'application/mac-binhex40', 'sit' => 'application/x-stuffit', 'bin' => 'application/x-macbinary', 'uu' => 'text/x-uuencode', 'uue' => 'text/x-uuencode', 'latex'=> 'application/x-latex', 'ltx' => 'application/x-latex', 'tcl' => 'application/x-tcl', 'pgp' => 'application/pgp', 'asc' => 'application/pgp', 'exe' => 'application/x-msdownload', 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'mdb' => 'application/x-msaccess', 'wri' => 'application/x-mswrite', ); ?>


Php excel file generation

 


Time comparison problems
For example, a forum marks a post posted on the day with a new image.
Method 1:

 Rows [$ I] [date] is the datetime field value in the database. $ today = time (); $ theDay = date ("Y-m-d H: I: s", $ today-24*3600 ); $ newTag = $ db-> rows [$ I] [date]> = $ theDay? "": ""; // Method 2: $ newTag = $ db-> rows [$ I] [date]> = date ("Y-m-d 00:00:00 ")? "": "";?>

// Extract the variables submitted by the page and browser, which is equivalent to enabling PHP. INI to enable global variables.

 

// Read the file function

 

// File writing function

 

// Quick page redirection

 NNPage turning ....N "; print"
 N "; print"N "; print"NN "; print"
 
 
  
  
N "; print"
  
  
     N "; print" 
    N "; print" 
   N "; print" 
   
     N "; print" 
    N "; print" 
   N "; print" 
   
     N "; print" 
    N "; print" 
   N "; print" 
  
N "; print" N "; print" N "; print" N "; print" N "; print" N "; print" N "; print" N "; print" N "; print" N "; print" N "; print"N "; print"
Page redirection prompt information
$ Info
N "; print" if your browser does not support automatic jump, click here
N "; print"N"; Exit;?>

Generate random string functions

 

Truncate a string of a certain length (this function is valid for GB2312)

  $length) { if($sss){ $length=$length - 3; $addstr=@# ...@#; } for($i = 0; $i < $length; $i++) { if(ord($string[$i]) > 127) { $wordscut .= $string[$i].$string[$i + 1]; $i++; } else { $wordscut .= $string[$i]; } } return $wordscut.$addstr; } return $string; } ?>

Obtain the client IP address

 


Determine email address

 

Paging (use with two functions)

 ". $ Totalpage ."Page "; $ linkNum = 4; $ start = ($ page-round ($ linkNum/2)> 0? ($ Page-round ($ linkNum/2): "1"; $ end = ($ page + round ($ linkNum/2) <$ totalpage? ($ Page + round ($ linkNum/2): $ totalpage; $ prestart = $ start-1; $ nextend = $ end + 1; if ($ page <> 1) $ out. = "first page"; if ($ start> 1) $ out. = ".. <"; for ($ t = $ start; $ t <= $ end; $ t ++) {$ out. = ($ page = $ t )? "[". $ T."]":" $ T ";} if ($ end <$ totalpage) $ out. = "> .. "; if ($ page <> $ totalpage) $ out. = "Last page"; return $ out;}?>


Obtain the ID of the newly inserted data

 


// Obtain the current script URL

 


// Convert the fullwidth number into a halfwidth number

 


// Remove HTML tags

 ",">",$txt); $txt = preg_replace("/[rn]{1,}/isU"," rn",$txt); return $txt; } ?>


// Convert relative paths to absolute paths

 

// Get all links

 ]*>([^>]+)/i',$code,$arr); return array('name'=>$arr[2],'url'=>$arr[1]); } ?>

// Convert each line of the HTML table into an array in CSV format

 ] *?> 'Si ", '"', $ table); $ table = str_replace ("", '",', $ Table); $ table = str_replace (""," {Tr} ", $ table); // remove the HTML tag $ table = preg_replace (" '<[/!] *? [^ <>] *?> 'Si "," ", $ table); // remove the blank character $ table = preg_replace (" '([rn]) [s] + '","", $ table); $ table = str_replace ("", "", $ table); $ table = str_replace ("", "", $ table ); $ table = explode (", {tr}", $ table); array_pop ($ table); return $ table ;}?>

// Convert each row and column of an HTML table into an array to collect table data

 ] *?> 'Si "," ", $ table); $ table = preg_replace ("'
 
  
] *?> 'Si "," ", $ table); $ table = preg_replace ("'
  
   
] *?> 'Si "," ", $ table); $ table = str_replace (""," {Tr} ", $ table); $ table = str_replace (""," {Td} ", $ table); // remove the HTML tag $ table = preg_replace (" '<[/!] *? [^ <>] *?> 'Si "," ", $ table); // remove the blank character $ table = preg_replace (" '([rn]) [s] + '","", $ table); $ table = str_replace ("", "", $ table); $ table = str_replace ("", "", $ table ); $ table = explode ('{tr}', $ table); array_pop ($ table); foreach ($ table as $ key => $ tr) {$ td = explode ('{td}', $ tr); array_pop ($ td); $ td_array [] = $ td;} return $ td_array;}?>
  
 

// Return all words in the string $ distinct = true to remove duplicates

 


// Print out a list of people contributing to this PHP project

 

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.