分享10段PHP常用代码
程序员文章站
2024-01-12 20:15:10
本文汇集php开发中经常用到的十段代码,包括email、64位编码和解码、解压缩、64位编码、解析json等,希望对您有所帮助。
1、使用php mail函数发送emai...
本文汇集php开发中经常用到的十段代码,包括email、64位编码和解码、解压缩、64位编码、解析json等,希望对您有所帮助。
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="﹤?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'; ?﹥
php常用功能如下
1.php字符串
字符串声明 变量=''或者""(一般情况会使用单引号,因为写起来会比较方便)
$str = 'hello php';
echo $str;
strpos 计算字符在字符串中的位置(从0开始)
$str = 'hello php';
echo strpos($str,'o'); //计算字符在字符串中的位置
echo '<br/>';
echo strpos($str,'ph');
substr 截取字符串
$str = 'hello php'; //截取字符串 $str1 = substr($str,2,3); //从2位置开始截取,截取长度为3的字符串 echo $str1;
不传入长度参数的话,会从指定位置一直截取到字符串的末尾
str_split 分割字符串 固定长度的分割(默认长度为1)
$str = 'hello php'; //分割字符串 $result = str_split($str); //将结果保存到一个数组中 print_r($result); //使用print_r输入一个数组 echo '<br/>'; $result1 = str_split($str,2); print_r($result1);
explode(分割字符,待分割的字符串) 按照空格进行分割
$str = 'hello php java c# c++'; $result = explode(' ',$str); print_r($result);
字符串的连接
$str = 'hello php java c# c++'; //字符串的连接 $num = 100; $str1 = $str.'<br/>objective-c '.$num; echo $str1; echo '<br/>'; $str2 = "$str<br/>objective-c $num"; //另一中简便的写法 echo $str2;
上一篇: json简介_JSON简介