PHP10段常用功能代码
时间:2022-07-23
本文章向大家介绍PHP10段常用功能代码,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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: Peterrn";
$headers .= "Reply-To: info@yoursite.comrn";
$headers .= "Return-Path: info@yoursite.comrn";
$headers .= "X-Mailer: PHP5n";
$headers .= 'MIME-Version: 1.0' . "n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
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.';
}
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';
?﹥
- 在ASP.NET MVC 中获取当前URL、controller、action
- [转自Scott]ASP.NET MVC框架(第四部分): 处理表单编辑和提交场景
- MVC前台Post/Get异步获得数据时参数的取值问题
- IQueryable与foreach的困惑?
- ModalPopupExtender用法示例
- MVC中局部视图的使用
- MVC发布后项目存在于根目录中的子目录中时的css与js、图片路径问题
- ORM中的继承关系映射全解——单表继承体系、一实体一具体表、一实体一扩展表、接口映射
- View与Control间的数据交互
- C#中Dispose和Close的区别!
- C#调用SQL中的存储过程中有output参数,存储过程执行过程中返回信息
- 你真的会玩SQL吗?查询指定节点及其所有父节点的方法
- ASP.Net MVC框架配置与分析
- MVC利用MvcHtmlString在后台生成HTML
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Linux下Apache HTTP Server 2.4.26安装教程
- 详解linux下查看系统版本号信息的方法(总结)
- 详解Centos8 配置静态IP的实现
- Linux server配置安装Java与Tomcat服务器教程详解
- Ubuntu系统下安装ImageMagick出错的解决过程
- linux 远程控制windows系统下的程序(三种方法)
- 详解Linux上svn命令行批量操作
- 详解linux ntp服务器时间同步设置
- CentOS设置静态IP的方法总结
- ubuntu服务器上快速部署docker的方法
- centos7使用supervisor的详细教程
- Ubuntu安装PHP和PHP Nginx配置方法
- Django在Ubuntu14.04的部署方法
- ubuntu19系统及以下版本安装android studio的教程
- CentOS7升级内核kernel5.0版本