成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

10段PHP常用功能代碼

原創
開發 后端
本文匯集PHP開發中經常用到的時段代碼,包括Email、解壓縮、64位編碼、解析JSON等,希望對您有所幫助。

1、使用PHP Mail函數發送Email

$to = "viralpatel.net@gmail.com";  
$subject = "VIRALPATEL.net";  
$body = "Body of your message here you can use HTML too. e.g. ﹤br﹥ ﹤b﹥ Bold ﹤/b﹥";  
$headers = "From: Peter\r\n";  
$headers .= "Reply-To: info@yoursite.com\r\n";  
$headers .= "Return-Path: info@yoursite.com\r\n";  
$headers .= "X-Mailer: PHP5\n";  
$headers .= 'MIME-Version: 1.0' . "\n";  
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
mail($to,$subject,$body,$headers);  
?﹥   

2、PHP中的64位編碼和解碼

function base64url_encode($plainText) {
	$base64 = base64_encode($plainText);
	$base64url = strtr($base64, '+/=', '-_,');
	return $base64url;
}

function base64url_decode($plainText) {
	$base64url = strtr($plainText, '-_,', '+/=');
	$base64 = base64_decode($base64url);
	return $base64;
} 

3、獲取遠程IP地址

function getRealIPAddr()
{
	if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
	{
		$ip=$_SERVER['HTTP_CLIENT_IP'];
	}
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
	{
		$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	else
	{
		$ip=$_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}

4、 日期格式化

function checkDateFormat($date)
{
	//match the format of the date
	if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
	{
		//check weather the date is valid of not
		if(checkdate($parts[2],$parts[3],$parts[1]))
			return true;
		else
		return false;
	}
	else
		return false;
}

5、驗證Email

$email = $_POST['email'];
if(preg_match("~([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~])@([a-zA-Z0-9-]).
                                 ([a-zA-Z0-9]{2,4})~",$email)) {
	echo 'This is a valid email.';
} else{
	echo 'This is an invalid email.';
} 

#p#

6、在PHP中輕松解析XML

//this is a sample xml string
$xml_string="﹤?xml version='1.0'?﹥
﹤moleculedb﹥
    ﹤molecule name='Benzine'﹥
        ﹤symbol﹥ben﹤/symbol﹥
        ﹤code﹥A﹤/code﹥
    ﹤/molecule﹥
    ﹤molecule name='Water'﹥
        ﹤symbol﹥h2o﹤/symbol﹥
        ﹤code﹥K﹤/code﹥
    ﹤/molecule﹥
﹤/moleculedb﹥";

//load the xml string using simplexml function
$xml = simplexml_load_string($xml_string);

//loop through the each node of molecule
foreach ($xml-﹥molecule as $record)
{
   //attribute are accessted by
   echo $record['name'], '  ';
   //node are accessted by -﹥ operator
   echo $record-﹥symbol, '  ';
   echo $record-﹥code, '﹤br /﹥';
}

7、數據庫連接

﹤?php
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404();
$dbHost = "localhost";        //Location Of Database usually its localhost
$dbUser = "xxxx";            //Database User Name
$dbPass = "xxxx";            //Database Password
$dbDatabase = "xxxx";       //Database Name

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or 
                                   die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

# This function will send an imitation 404 page if the user
# types in this files filename into the address bar.
# only files connecting with in the same directory as this
# file will be able to use it as well.
function send_404()
{
    header('HTTP/1.x 404 Not Found');
    print '﹤!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"﹥'."n".
    '﹤html﹥﹤head﹥'."n".
    '﹤title﹥404 Not Found﹤/title﹥'."n".
    '﹤/head﹥﹤body﹥'."n".
    '﹤h1﹥Not Found﹤/h1﹥'."n".
    '﹤p﹥The requested URL '.
    str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']).
    ' was not found on this server.﹤/p﹥'."n".
    '﹤/body﹥﹤/html﹥'."n";
    exit;
}

# In any file you want to connect to the database,
# and in this case we will name this file db.php
# just add this line of php code (without the pound sign):
# include"db.php";
?﹥

8、創建和解析JSON數據

$json_data = array ('id'=﹥1,'name'=﹥"rolf",'country'=﹥'russia',
			"office"=﹥array("google","oracle"));
echo json_encode($json_data);

9、處理MySQL時間戳

$query = "select UNIX_TIMESTAMP(date_field) as mydate 
			    from mytable where 1=1";
$records = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($records))
{
	echo $row;
} 

10、解壓縮Zip文件

﹤?php
    function unzip($location,$newLocation){
        if(exec("unzip $location",$arr)){
            mkdir($newLocation);
            for($i = 1;$i﹤ count($arr);$i++){
                $file = trim(preg_replace("~inflating: ~","",$arr[$i]));
                copy($location.'/'.$file,$newLocation.'/'.$file);
                unlink($location.'/'.$file);
            }
            return TRUE;
        }else{
            return FALSE;
        }
    }
?﹥
//Use the code as following:
﹤?php
include 'functions.php';
if(unzip('zipedfiles/test.zip','unziped/myNewZip'))
    echo 'Success!';
else
    echo 'Error';
?﹥

【編輯推薦】

  1. 20個對開發人員非常有用的Java代碼片段
  2. PHP 6預覽 新增多項特性及改進
  3. 國外十大***PHP框架排名
責任編輯:佚名 來源: 51CTO.com
相關推薦

2010-07-28 15:42:44

Flex

2019-09-04 14:30:54

Nginx功能服務器

2011-02-22 09:08:14

vsFTPd

2011-02-22 09:55:00

vsFTPd

2011-03-01 14:00:16

vsFTPd功能

2013-08-20 16:14:46

pythonpython文本處理

2011-02-22 09:40:42

vsFTPd

2011-07-07 17:27:54

PHP

2011-07-07 17:24:28

PHP

2011-02-22 10:12:07

vsFTPd

2011-10-08 13:54:27

JavaScript

2010-02-03 17:39:21

2010-02-03 09:58:33

全光交換機

2021-12-15 09:44:36

Windows 11Windows微軟

2018-02-26 11:25:33

2011-02-22 09:23:21

vsFTPd

2016-06-29 13:50:12

云計算

2011-02-21 18:11:27

vsFTPd

2010-02-25 16:12:23

WCF IDispos

2023-10-27 08:59:00

網絡wiresharkIO
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲乱码国产乱码精品精的特点 | 亚洲第一黄色网 | 黄色片在线网站 | 亚洲三级av| 中文字幕免费 | 一级片在线播放 | 亚洲天堂免费 | 久久久久av | 超碰国产在线 | 欧美日韩国产三级 | 黄色毛片在线播放 | 日韩成人中文字幕 | 久久久久久免费观看 | 人操人人 | 久久精品日产第一区二区三区 | 精品国产青草久久久久福利 | 欧美日韩国产在线观看 | 国产婷婷精品av在线 | 欧美综合视频 | 97av视频在线观看 | 欧美淫片 | 欧美一级三级在线观看 | 91免费观看 | a级黄色片在线观看 | 精品在线一区二区 | 在线观看国产三级 | 一级做a爰片性色毛片16 | 精品免费国产视频 | 91麻豆精品国产91久久久更新资源速度超快 | 欧美日韩不卡 | 久久久91精品国产一区二区三区 | 日本精品一区二区三区四区 | 精品久久国产 | 草草网| 一区二区三区免费 | 一级毛片免费视频观看 | 亚洲欧美少妇 | 精品久久久久久久久久久久久 | 亚洲成人观看 | 国产精品成人一区二区三区 | 精品熟人一区二区三区四区 |