PHP开发常用的10段代码_第1页
PHP开发常用的10段代码_第2页
PHP开发常用的10段代码_第3页
PHP开发常用的10段代码_第4页
PHP开发常用的10段代码_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

本文格式为Word版,下载可任意编辑——PHP开发常用的10段代码PHP是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,下文是为大家精选的PHP开发常用的10段代码,接待大家阅读。

1、使用PHPMail函数发送Email

$to=@;

$subject=VIRALPATEL.net;

$body=BodyofyourmessagehereyoucanuseHTMLtoo.e.g.﹤br﹥﹤b﹥Bold﹤/b﹥;

$headers=From:Peter;

$headers.=Reply-To:info@;

$headers.=Return-Path:info@;

$headers.=X-Mailer:PHP5;

$headers.=MIME-Version:1.0.;

$headers.=Content-type:text/html;charset=iso-8859-1.;

mail$to,$subject,$body,$headers;

?﹥

2、PHP中的64位编码和解码

functionbase64url_encode$plainText

$base64=base64_encode$plainText;

$base64url=strtr$base64,+/=,-_,;

return$base64url;

functionbase64url_decode$plainText

$base64url=strtr$plainText,-_,,+/=;

$base64=base64_decode$base64url;

return$base64;

3、获取远程IP地址

functiongetRealIPAddr

if!empty$_SERVER[HTTP_CLIENT_IP]//checkipfromshareinternet

$ip=$_SERVER[HTTP_CLIENT_IP];

elseif!empty$_SERVER[HTTP_X_FORWARDED_FOR]//tocheckipispassfromproxy

$ip=$_SERVER[HTTP_X_FORWARDED_FOR];

else

$ip=$_SERVER[REMOTE_ADDR];

return$ip;

4、日期格式化

functioncheckDateFormat$date

//matchtheformatofthedate

ifpreg_match/^[0-9]4-[0-9]2-[0-9]2$/,$date,$parts

//checkweatherthedateisvalidofnot

ifcheckdate$parts[2],$parts[3],$parts[1]

returntrue;

else

returnfalse;

else

returnfalse;

5、验证Email

$email=$_POST[email];

ifpreg_match~[a-zA-Z0-9!#$%*+-/=?^_`|~]@[a-zA-Z0-9-].

[a-zA-Z0-9]2,4~,$email

echoThisisavalidemail.;

else

echoThisisaninvalidemail.;

6、在PHP中轻松解析XML

//thisisasamplexmlstring

$xml_string=﹤?xmlversion=1.0?﹥

﹤moleculedb﹥

﹤moleculename=Benzine﹥

﹤symbol﹥ben﹤/symbol﹥

﹤code﹥A﹤/code﹥

﹤/molecule﹥

﹤moleculename=Water﹥

﹤symbol﹥h2o﹤/symbol﹥

﹤code﹥K﹤/code﹥

﹤/molecule﹥

﹤/moleculedb﹥;

//loadthexmlstringusingsimplexmlfunction

$xml=simplexml_load_string$xml_string;

//loopthroughtheeachnodeofmolecule

foreach$xml-﹥moleculeas$record

//attributeareaccesstedby

echo$record[name],;

//nodeareaccesstedby-﹥operator

echo$record-﹥symbol,;

echo$record-﹥code,﹤br/﹥;

7、数据库连接

﹤?php

ifbasename__FILE__==basename$_SERVER[PHP_SELF]send_404;

$dbHost=localhost;//LocationOfDatabaseusuallyitslocalhost

$dbUser=xxxx;//DatabaseUserName

$dbPass=xxxx;//DatabasePassword

$dbDatabase=xxxx;//DatabaseName

$db=mysql_connect$dbHost,$dbUser,$dbPassor

dieErrorconnectingtodatabase.;

mysql_select_db$dbDatabase,$dbordieCouldntselectthedatabase.;

#Thisfunctionwillsendanimitation404pageiftheuser

#typesinthisfilesfilenameintotheaddressbar.

#onlyfilesconnectingwithinthesamedirectoryasthis

#filewillbeabletouseitaswell.

functionsend_404

headerHTTP/1.x404NotFound;

print﹤!DOCTYPEHTMLPUBLIC-//IETF//DTDHTML2.0//EN﹥.n.

﹤html﹥﹤head﹥.n.

﹤title﹥404NotFound﹤/title﹥.n.

﹤/head﹥﹤body﹥.n.

﹤h1﹥NotFound﹤/h1﹥.n.

﹤p﹥TherequestedURL.

str_replacestrstr$_SERVER[REQUEST_URI],?,,$_SERVER[REQUEST_URI].

wasnotfoundonthisserver.﹤/p﹥.n.

﹤/body﹥﹤/html﹥.n;

exit;

#Inanyfileyouwanttoconnecttothedatabase,

#andinthiscasewewillnamethisfiledb.php

#justaddthislineofphpcodewithoutthepoundsign:

#includedb.php;

?﹥

8、创造和解析JSON数据

$json_data=arrayid=﹥1,name=﹥rolf,country=﹥russia,

office=﹥arraygoogle,oracle;

echojson_encode$json_data;

9、处理MySQL时间戳

$query=selectUNIX_TIMESTAMPdate_fieldasmydate

frommytablewhere1=1;

$records=mysql_query$queryordiemysql_error;

while$row=mysql_fetch_array$records

echo$row;

10、解压缩Zip文件

﹤?php

functionunzip$location,$newLocation

ifexecunzip$location,$arr

mkdir$newLocation;

for$i=1;$i﹤count$arr;$i++

$file=trimpreg_replace~inflating:~,,$arr[$i];

copy$location./.$file,$newLocation./.$file;

unlink$location./.$file;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论