10段PHP常用功能代码
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.';
}
6、在PHP中轻松解析XML
//this is a sample xml string
$xml_string="﹤?x
相关文档:
在PHP中有urlencode()、urldecode()、rawurlencode()、rawurldecode()这些函数来解决网页
URL编码解码问题。
在ASP的时候URL编码解码很是恼火,Server.urlencode不太好用,遇到utf-8编码的地址更是麻
烦。你要获取百度、Google点击到网站的网址链接中的关键字,要写上一堆自定义函数来得到urldecode的效果。
摘录一篇关 ......
可以使用的命令:
popen
fpassthru
shell_exec
exec
system
1.popen
resource popen
( string command, string mode )
打开一个指向进程的管道,该进程由派生给定的 command
命令执行而产生。
返回一个和 fopen()
所返回的相同的文件指针,只不过它是单向的(只能用于读或写)并且必须用 pclose()
来关闭 ......
一:结构和调用(实例化):
class className{} ,调用:$obj = new className();当类有构造函数时,还应传入参数。如$obj = new className($v,$v2...);
二:构造函数和析构函数:
1、构造函数用于初始化:使用__construct(),可带参数。
2、但析构函数不能带参数(用于在销去一个类之前执行一些操作或功能)。析构函数 ......
第一章 PHP概述
1、 基本语法
a) 要把php嵌入页面,可以把它放在PHP标签内
<?php ?>
b) 在body标签结束之前,插入php代码
c)   ......
php导入到excel-支持utf8和gbk两种编码
php导入到excel乱码是因为utf8编码在xp系统不支持所有utf8编码转码一下就完美解决了
utf-8编码案例
<?php
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Pragma: public");
header("Expires: 0");
header("Cache ......