php是一种通用开源脚本语言。语法吸收了c语言、java和perl的特点,下文是为大家精选的php开发常用的10段代码,欢迎大家阅读。 1、使用phpmail函数发送email$to=下面是小编为大家整理的2023年度php开发常用10段代码(2023年),供大家参考。
php是一种通用开源脚本语言。语法吸收了c语言、java和perl的特点,下文是为大家精选的php开发常用的10段代码,欢迎大家阅读。
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";
$headers .= "reply-to: info@yoursite.com";
$headers .= "return-path: info@yoursite.com";
$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位编码和解码
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
ifcheckdate$parts[2],$parts[3],$parts[1]
return true;
else
return false;
else
return false;
5、验证email
$email = $_post['email'];
ifpreg_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
ifbasename__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_replacestrstr$_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_timestampdate_field as mydate
from mytable where 1=1";
$records = mysql_query$query or diemysql_error;
while$row = mysql_fetch_array$records
echo $row;
10、解压缩zip文件
﹤?php
function unzip$location,$newlocation
ifexec"unzip $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;
return true;
else
return false;
?﹥
//use the code as following:
﹤?php
include 'functions.php';
ifunzip'zipedfiles/test.zip','unziped/mynewzip'
echo 'success!';
else
echo 'error';
?﹥
推荐访问:常用