[PHP] tutorials on page 1/2 _ PHP, a collection of commonly used practical functions

Source: Internet
Author: User
Tags add time date1
[PHP] 12th pages of a set of commonly used practical functions. I am going to learn about php. I found that many functions are needed and can be written by myself, which is troublesome. so I found common php functions and obtained them from various major php systems, php is definitely the latest practical function used to learn php. I found that many functions need to be used and can be written by myself, which is troublesome. so I found a common function in php, from various major php systems, php practical functions are definitely used.

The code is as follows:


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 ();


...

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.