【ZT】10段PHP常用功能代码

2016-07-07 16:50 来源:www.chinab4c.com 作者:ecshop专家

1、使用PHP Mail函数发送Email, ]# \+^" _3f&b& |-\;J$to = "viralpatel.net@gmail.com";5 _1d) \2H,r1j*r:^
$subject = "VIRALPATEL.net";' T'A" _.{2p7u"T6A* |
$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";# {1k3K-v0K-i-m!k
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";: U9Y2H1[3|"/V3A* |
mail($to,$subject,$body,$headers);1 Q+D1y4B$b
?﹥0 G4w%?,@#f%|*h

2、PHP中的64位编码和解码2z6~:I$D+__function base64url_encode($plainText) {v" U)u;C/\" |$r
$base64 = base64_encode($plainText);8 [2T2}2_,F:F
$base64url = strtr($base64, '+/=', '-_,');9 A.{%B*y4s'o$W
return $base64url;' g"d"r2o)u5{9@-{4f
}
4|2\*V1j5v2V) ^5d9@+K%}
function base64url_decode($plainText) {
$base64url = strtr($plainText, '-_,', '+/=');, e' ]-F6a#J
$base64 = base64_decode($base64url);+k:F& |$N2v"U)p
return $base64;8 j$G&R(c/z"y3r
}聽

3、获取远程IP地址, `0K-@._9|2}:W3nZZfunction getRealIPAddr()3 V/n9~6@* {,]
{v: R&O5m*t8u!g
if (!empty($_SERVER['HTTP_CLIENT_IP']))//check ip from share internet3 [6H7K:H,k3]
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}& O0y"u3C:w#S)H
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))//to check ip is pass from proxy. b3f#h&d! _( [
{5 W1Y&g0m" {$Y9U2x
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;* K)L6u2S"p6O9o
}, K+^/D6X!o"M;I!X

4、 日期格式化function checkDateFormat($date)2 i,N%H"T!s:f.o!Y+?)a
{
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))6 N9h-A,r"T;l
{2 x,i;y$y%[)j2R;Iq
//check weather the date is valid of not1 l5p1h3s1}(d2p
if(checkdate($parts[2],$parts[3],$parts[1])); N0u( \$R"z:a0Y+W8L
return true;
else
return false;
}$ X& ?3Y%b;M6Y
else' t;v$N) ^8?%C/b2~"B! \
return false;
}' _; X0Nx:B4_N%l

5、验证Email# ]1 }& u+l/]3R$email = $_POST['email'];7 o5f7~/Q.d%@"k5d.f
if(preg_match("~([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~])@([a-zA-Z0-9-])./ N*w3F)I)Q(S) ^/O+z4W
聽([a-zA-Z0-9]{2,4})~",$email)) {
echo 'This is a valid email.';9 Z&F)F6}+r"K$@*V( ?+E
} else{
echo 'This is an invalid email.';
} ; j!f+g3d1|"q*n

6、在PHP中轻松解析XML
/v,gi3[&z$h
//this is a sample xml string+t4A:s(z-p-D!v4|
$xml_string="﹤?xml version='1.0'?﹥; H)ra*i;l) \4K!v
﹤moleculedb﹥: I;i2z-d-N5b
聽﹤molecule name='Benzine'﹥4V-E)m%Q5W-D,j
聽 ﹤symbol﹥ben﹤/symbol﹥1t%W'N9o,a1R9{
聽 ﹤code﹥A﹤/code﹥1U) |0s:_,W.p"A
聽﹤/molecule﹥
聽﹤molecule name='Water'﹥) [k.H#Z4^1d5z) _4|
聽 ﹤symbol﹥h2o﹤/symbol﹥6Y:},g*h)Z
聽 ﹤code﹥K﹤/code﹥) Y! \/x5Z.R"'I
聽﹤/molecule﹥
﹤/moleculedb﹥";

//load the xml string using simplexml function0 O4f%J* ^4\F(T(p
$xml = simplexml_load_string($xml_string);
4}:g*C%^%Z6D6C
//loop through the each node of molecule. E0_'B6K'c,u!v&e' ?&m-j
foreach ($xml-﹥molecule as $record)0 H$z&W7`'w/m0|9m
{' J5T(y8w1r"S'A#|/Y
聽//attribute are accessted by7 \9S'z9m,n*m-V( [
聽echo $record['name'], '';& r/X~4V4s"C1_)W,S
聽//node are accessted by -﹥ operator! M,l#\9F' @-f2O( ]'C
聽echo $record-﹥symbol, '';6 e-G'j0W,C7B
聽echo $record-﹥code, '﹤br /﹥';" v5B2e(o!y!f#j2t
}/ K#U*F;M%q*G( `

:Z"O(g' ?7`'h&W" ?)Z
7、数据库连接- {.n!n&u2g7ct&l,N

﹤?php
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404();) Q6U+J!h7r
$dbHost = "localhost";//Location Of Database usually its localhost# h*L"d$BE#I.w3N1e&O
$dbUser = "xxxx";//Database User Name
$dbPass = "xxxx";//Database Password5 h1F'p.|3l4X
$dbDatabase = "xxxx"; //Database Name7 S'r8y5]$a7l" {
3g'o7f#A'b,L
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or ; I#}3U0X4f8B-F0_; ?
聽 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.0 @8a7~2Z/^4t6B,F
# only files connecting with in the same directory as this
# file will be able to use it as well.
function send_404()
{% O" ?4q4T.|-M
聽header('HTTP/1.x 404 Not Found');
聽print '﹤!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"﹥'."n".
聽'﹤html﹥﹤head﹥'."n".q+V;K.?(o)S7u
聽'﹤title﹥404 Not Found﹤/title﹥'."n".' `+JK)S-U.S
聽'﹤/head﹥﹤body﹥'."n".- ]1 y,X,]/x! ]/a5h+J4Y
聽'﹤h1﹥Not Found﹤/h1﹥'."n".0 d5R+a'Z,N-V
聽'﹤p﹥The requested URL '.; B%i1f:h1\; [
聽str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']).
聽' was not found on this server.﹤/p﹥'."n".
聽'﹤/body﹥﹤/html﹥'."n";
聽exit;2 M2m*R9o;F,T$J*E
}

# In any file you want to connect to the database,) G(W/G%D3}2L*u1B1F'b7C
# and in this case we will name this file db.php
# just add this line of php code (without the pound sign):! l%c3y8R2e3J1r5C-Y5A
# include"db.php";$E*W0Y#t;j0X5x5[.y"E
?﹥
(Nz#I#l!W1p
:R0V9_(r+C,K*i's7x.l8w
8、创建和解析JSON数据

$json_data = array ('id'=﹥1,'name'=﹥"rolf",'country'=﹥'russia',8 G$c#ER0{2I
"office"=﹥array("google","oracle"));(R+O&G/o6{U)aI
echo json_encode($json_data);
6}7T5C:w1m!z/\
:k*H5a&J2L9P&V
9、处理MySQL时间戳#y+n.i(o5M'a.E/J
,P+w$U1j1E$N8W7I2H
$query = "select UNIX_TIMESTAMP(date_field) as mydate聽
聽from mytable where 1=1";* m3G)Q&A.Z;w; \$`&a
$records = mysql_query($query) or die(mysql_error());+`#r(F; @4T:C9b8o!F-@
while($row = mysql_fetch_array($records))
{
echo $row;6 m'j:j/{%n
}聽
;D2U*D'n(T6h,@7|+r&q5D
2@"a$p;q9|+]5B-R'C
10、解压缩Zip文件
2n/C0e.U2R:s%^8A.t4X
﹤?php
聽function unzip($location,$newLocation){
聽 if(exec("unzip $location",$arr)){
聽mkdir($newLocation);(u/f!u1I)Z%a
聽for($i = 1;$i﹤ count($arr);$i++){4 Q;x'n6Y$T2V-y
聽$file = trim(preg_replace("~inflating: ~","",$arr[$i]));
聽copy($location.'/'.$file,$newLocation.'/'.$file);
聽unlink($location.'/'.$file);; P1n(d6d"r(I:T
聽}
聽return TRUE;. O$D! ])C7X:p* |:^5r!f.O
聽 }else{# c%W-`4k6x2F
聽return FALSE;9 w& @5`8Hg:{; ~
聽 }0 |2w*w$N$v.e2i
聽}+ T)C"I(V+l/D"Q
?﹥
//Use the code as following:
﹤?php0 Az/`m#]#h
include 'functions.php';
if(unzip('zipedfiles/test.zip','unziped/myNewZip'))/ C8w8C' `%w&d
聽echo 'Success!';3 h$q)B:h+e1n:Y
else
聽echo 'Error';
?﹥

回答:
:lol 谢谢了

赞一个!!呵呵,支持楼主!