前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP10段常用功能代码

PHP10段常用功能代码

作者头像
用户7657330
发布2020-08-14 15:32:22
6350
发布2020-08-14 15:32:22
举报
文章被收录于专栏:程序生涯程序生涯

1、使用PHP Mail函数发送Email

代码语言:javascript
复制
$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位编码和解码

代码语言:javascript
复制
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地址

代码语言:javascript
复制
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、 日期格式化

代码语言:javascript
复制
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

代码语言:javascript
复制
$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.';

			}

6、在PHP中轻松解析XML

代码语言:javascript
复制
//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、数据库连接

代码语言:javascript
复制
﹤?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数据

代码语言:javascript
复制
$json_data = array ('id'=﹥1,'name'=﹥"rolf",'country'=﹥'russia',

"office"=﹥array("google","oracle"));

echo json_encode($json_data);

9、处理MySQL时间戳

代码语言:javascript
复制
$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文件

代码语言:javascript
复制
﹤?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';

?﹥
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档