[PHP] frequently used in the collection of practical functions 1th/2 page _php Tutorial

Source: Internet
Author: User
Tags ereg get ip
The latest plan to learn PHP, found a lot of functions need to use, can write their own trouble, so find the next PHP commonly used functions, from the big PHP system down, is definitely the use of PHP utility function
Copy CodeThe code is as follows:
function GetIP () {//Get IP
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") {//Time increase (also can be improved into seconds and minutes can be increased, time to fill in)
$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) {//calculates the date given is the day of the week
$DATEARR = Explode ("-", $date);
Return Date ("W", Mktime (0,0,0, $DATEARR [1], $DATEARR [2], $DATEARR [0]));
}

Copy CodeThe code is as follows:
function Check_date ($date) {//check date is legal date
$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 legitimate
$TIMEARR = Explode (":", $time);
if (is_numeric ($timeArr [0]) && is_numeric ($TIMEARR [1]) && is_numeric ($TIMEARR [2])) {
if ($timeArr [0] >= 0 && $timeArr [0] <= && ($TIMEARR [1] >= 0 && $timeArr [1] <= 59) && ($TIMEARR [2] >= 0 && $timeArr [2] <= 59))
return true;
Else
return false;
}
return false;
}

function DateDiff ($date 1, $date 2, $unit = "") {//Time comparison function, return two dates in seconds, minutes, hours or days
Switch ($unit) {
Case ' s ':
$dividend = 1;
Break
Case ' I ':
$dividend = 60;
Break
Case ' H ':
$dividend = 3600;
Break
Case ' d ':
$dividend = 86400;
Break
Default
$dividend = 86400;
}
$time 1 = strtotime ($date 1);
$time 2 = strtotime ($date 2);
if ($time 1 && $time 2)
return (float) ($time 1-$time 2)/$dividend;
return false;
}

Copy CodeThe 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 "); Would print "something".

function Get_input_value ($input) {//Gets the value in the HTML code of 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) {//Get string $str, string $ A string between string $b
if ($str!== "" && $a!== "" && $b!== "") {
$start = Strpos ($str, $a) + strlen ($a);
Return substr ($str, $start, Strpos ($str, $b, $start + 1)-$start);
}
return false;
}


1. About PHP Redirection
Copy CodeThe code is as follows:
Method One: Header ("Location:index.php");
Method Two: Echo " window.location = "$PHP _self"; ";
Method Three: Echo " ";

2. Get the visitor's browser
Copy CodeThe 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 (); return results directly

3. Get the visitor's operating system
Copy CodeThe 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 (' + ', $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 (' NetBSD ', $Agent)) {
$os = "NetBSD";
}
ElseIf (eregi (' BSD ', $Agent)) {
$os = "BSD";
}
ElseIf (Ereg (' OSF1 ', $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 ();

http://www.bkjia.com/PHPjc/318394.html www.bkjia.com true http://www.bkjia.com/PHPjc/318394.html techarticle The latest plan to learn PHP, found a lot of functions need to use, can write their own trouble, so find the next PHP common functions, from the big PHP system down, is definitely the use of PHP utility function ...

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