[PHP] a set of commonly used practical functions

Source: Internet
Author: User
Tags add time gtar
[PHP] a collection of frequently used practical functions is about to learn php. I found that many functions need to be used and can be written by myself, so I found common php functions, from various major php systems, php practical functions are definitely used.

Function GetIP () {// obtain the IP address
If ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])
$ Ip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];
Else if ($ _ SERVER ["HTTP_CLIENT_IP"])
$ Ip = $ _ SERVER ["HTTP_CLIENT_IP"];
Else if ($ _ SERVER ["REMOTE_ADDR"])
$ Ip = $ _ SERVER ["REMOTE_ADDR"];
Else if (getenv ("HTTP_X_FORWARDED_FOR "))
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
Else if (getenv ("HTTP_CLIENT_IP "))
$ Ip = getenv ("HTTP_CLIENT_IP ");
Else if (getenv ("REMOTE_ADDR "))
$ Ip = getenv ("REMOTE_ADDR ");
Else
$ Ip = "Unknown ";
Return $ ip;
}

Function DateAdd ($ date, $ int, $ unit = "d") {// increase of time (you can also improve it to increase by hour, minute, and second, and then add time)
$ DateArr = explode ("-", $ date );
$ Value [$ unit] = $ int;
Return date ("Y-m-d", mktime (0, 0, 0, $ dateArr [1] + $ value ['M'], $ dateArr [2] + $ value ['D'], $ dateArr [0] + $ value ['Y']);
}

Function GetWeekDay ($ date) {// calculate the day of the week
$ DateArr = explode ("-", $ date );
Return date ("w", mktime (0, 0, 0, $ dateArr [1], $ dateArr [2], $ dateArr [0]);
}


The code is as follows:


Function check_date ($ date) {// check whether the date is valid
$ DateArr = explode ("-", $ date );
If (is_numeric ($ dateArr [0]) & is_numeric ($ dateArr [1]) & is_numeric ($ dateArr [2]) {
Return checkdate ($ dateArr [1], $ dateArr [2], $ dateArr [0]);
}
Return false;
}
Function check_time ($ time) {// check whether the time is valid
$ TimeArr = explode (":", $ time );
If (is_numeric ($ timeArr [0]) & is_numeric ($ timeArr [1]) & is_numeric ($ timeArr [2]) {
If ($ timeArr [0]> = 0 & $ timeArr [0] <= 23) & ($ timeArr [1]> = 0 & $ timeArr [1] <= 59) & ($ timeArr [2]> = 0 & $ timeArr [2] <= 59 ))
Return true;
Else
Return false;
}
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:
$ Pidend = 86400;
}
$ Time1 = strtotime ($ date1 );
$ Time2 = strtotime ($ date2 );
If ($ time1 & $ time2)
Return (float) ($ time1-$ time2)/$ pidend;
Return false;
}


The code is as follows:


Function get_innerhtml ($ html, $ label) {// gets an html string between html tags.
$ Result_arr = preg_split ("/<\/". $ label. ">/I", $ html );
$ Pattern = "/<". $ label. ". *?> /I ";
For ($ I = 0; $ I <count ($ result_arr); $ I ++ ){
List ($ left, $ right) = preg_split ($ pattern, $ result_arr [$ I], 2 );
$ Result_arr [$ I] = $ right;
}
Return $ result_arr;
}
// Example: echo get_innerhtml ("Something"," Td "); // will print" something ".

Function get_input_value ($ input) {// obtain the Value in the HTML code of the Input.
$ Pos = stripos ($ input, "value =") + 6;
If ($ pos! = False ){
$ Input = substr ($ input, $ pos );
If (substr ($ input, 0, 1) = "\"")
Return substr ($ input, 1, strpos ($ input, "\" ", 1)-1 );
Else
Return substr ($ input, 0, strpos ($ input, "")-1 );
}
Return false;
}

Function getcontentbetween ($ a, $ B, $ str) {// Obtain the string between $ a and $ B in string $ str.
If ($ str! = "" & $! = "" & $ B! = ""){
$ Start = strpos ($ str, $ a) + strlen ($ );
Return substr ($ str, $ start, strpos ($ str, $ B, $ start + 1)-$ start );
}
Return false;
}


1. PHP redirection

The code is as follows:


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


2. obtain the visitor's browser

The code is as follows:


Function browse_infor ()
{
$ Browser = ""; $ browserver = "";
$ Browsers = array ("Lynx", "MOSAIC", "AOL", "Opera", "JAVA", "MacWeb", "WebExplorer", "OmniWeb ");
$ Agent = $ GLOBALS ["HTTP_USER_AGENT"];
For ($ I = 0; $ I <= 7; $ I ++)
{
If (strpos ($ Agent, $ Browsers [$ I])
{
$ Browser = $ Browsers [$ I];
$ Browserver = "";
}
}
If (ereg ("Mozilla", $ Agent )&&! Ereg ("MSIE", $ Agent ))
{
$ Temp = explode ("(", $ Agent); $ Part = $ temp [0];
$ Temp = explode ("/", $ Part); $ browserver = $ temp [1];
$ Temp = explode ("", $ browserver); $ browserver = $ temp [0];
$ Browserver = preg_replace ("/([d.] +)/", "1", $ browserver );
$ Browserver = "$ browserver ";
$ Browser = "Netscape Navigator ";
}
If (ereg ("Mozilla", $ Agent) & ereg ("Opera", $ Agent ))
{
$ Temp = explode ("(", $ Agent); $ Part = $ temp [1];
$ Temp = explode (")", $ Part); $ browserver = $ temp [1];
$ Temp = explode ("", $ browserver); $ browserver = $ temp [2];
$ Browserver = preg_replace ("/([d.] +)/", "1", $ browserver );
$ Browserver = "$ browserver ";
$ Browser = "Opera ";
}
If (ereg ("Mozilla", $ Agent) & ereg ("MSIE", $ Agent ))
{
$ Temp = explode ("(", $ Agent); $ Part = $ temp [1];
$ Temp = explode (";", $ Part); $ Part = $ temp [1];
$ Temp = explode ("", $ Part); $ browserver = $ temp [2];
$ Browserver = preg_replace ("/([d.] +)/", "1", $ browserver );
$ Browserver = "$ browserver ";
$ Browser = "Internet Explorer ";
}
If ($ browser! = "")
{
$ Browseinfo = "$ browser $ browserver ";
}
Else
{
$ Browseinfo = "Unknown ";
}
Return $ browseinfo;
}
// Call method $ browser = browseinfo (); directly return the result


3. obtain the visitor's operating system

The code is as follows:


Function osinfo (){
$ OS = "";
$ Agent = $ GLOBALS ["HTTP_USER_AGENT"];
If (eregi ('win', $ Agent) & strpos ($ Agent, '95 ')){
$ OS = "Windows 95 ";
}
Elseif (eregi ('win 9x', $ Agent) & strpos ($ Agent, '4. 90 ')){
$ OS = "Windows ME ";
}
Elseif (eregi ('win', $ Agent) & ereg ('98 ', $ Agent )){
$ OS = "Windows 98 ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt 5.0 ', $ Agent )){
$ OS = "Windows 2000 ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt ', $ Agent )){
$ OS = "Windows NT ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt 5.1 ', $ Agent )){
$ OS = "Windows XP ";
}
Elseif (eregi ('win', $ Agent) & ereg ('32', $ Agent )){
$ OS = "Windows 32 ";
}
Elseif (eregi ('Linux ', $ Agent )){
$ OS = "Linux ";
}
Elseif (eregi ('unix ', $ Agent )){
$ OS = "Unix ";
}
Elseif (eregi ('sun', $ Agent) & eregi ('OS', $ Agent )){
$ OS = "SunOS ";
}
Elseif (eregi ('IBM ', $ Agent) & eregi (' OS ', $ Agent )){
$ OS = "ibm OS/2 ";
}
Elseif (eregi ('Mac', $ Agent) & eregi ('PC', $ Agent )){
$ OS = "Macintosh ";
}
Elseif (eregi ('powerpc', $ Agent )){
$ OS = "PowerPC ";
}
Elseif (eregi ('aix ', $ Agent )){
$ OS = "AIX ";
}
Elseif (eregi ('hpux ', $ Agent )){
$ OS = "HPUX ";
}
Elseif (eregi ('netsd', $ Agent )){
$ OS = "NetBSD ";
}
Elseif (eregi ('bsd', $ Agent )){
$ OS = "BSD ";
}
Elseif (ereg ('oss1', $ Agent )){
$ OS = "OSF1 ";
}
Elseif (ereg ('irix ', $ Agent )){
$ OS = "IRIX ";
}
Elseif (eregi ('freebsd ', $ Agent )){
$ OS = "FreeBSD ";
}
If ($ OS = '') $ OS =" Unknown ";
Return $ OS;
}
// Call method $ OS = OS _infor ();



4. file format
Copy the content to the clipboard code:
$ Mime_types = array (
'Gif' => '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-Forwarded Cr limit pt ',
'PDF '=> 'application/pdf ',
'Ps' => 'application/postscr reject pt ',
'Eps '=> 'application/postscr reject pt ',
'AI' => 'application/postscr reject 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 ',

'Av' => '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-uencode ',
'Uuue' => 'text/x-uencode ',

'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 ',
);
5. generate an excel file in php

The code is as follows:


Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: filename=test.xls ");
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
Echo "test1t ";
Echo "test2tn ";
?>
// You can change the corresponding file header to export file formats such as .doc. xls.



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

The code is as follows:


// $ Db-> rows [$ I] [date] indicates 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:

The code is as follows:


$ 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.
Copy the content to the clipboard code:
@ Extract ($ _ SERVER, EXTR_SKIP );
@ Extract ($ _ SESSION, EXTR_SKIP );
@ Extract ($ _ POST, EXTR_SKIP );
@ Extract ($ _ FILES, EXTR_SKIP );
@ Extract ($ _ GET, EXTR_SKIP );
@ Extract ($ _ ENV, EXTR_SKIP );
// Obtain the IP address
$ Ip = $ _ SERVER ['remote _ ADDR '];
$ Ip1 = getenv ('http _ X_FORWARDED_FOR ');
If ($ ip1! = "") & ($ Ip1! = "Unknown") $ ip = $ ip1;
// Read the file function


Function readfromfile ($ file_name ){
If (file_exists ($ file_name )){
$ Filenum = fopen ($ file_name, "r ");
Flock ($ filenum, LOCK_EX );
$ File_data = fread ($ filenum, filesize ($ file_name ));
Rewind ($ filenum );
Fclose ($ filenum );
Return $ file_data;
}
}
// File writing function
Copy the content to the clipboard code:

The code is as follows:


Function writetofile ($ file_name, $ data, $ method = "w "){
$ Filenum = fopen ($ file_name, $ method );
Flock ($ filenum, LOCK_EX );
$ File_data = fwrite ($ filenum, $ data );
Fclose ($ filenum );
Return $ file_data;
}
// Quick page redirection
Copy the content to the clipboard code:
Function turntopage ($ url = "index. php", $ info = "page redirection...", $ second = 2 ){
Print"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;

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.