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 dateif (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts)){//check weather the date is valid of notif(checkdate($parts[2],$parts[3],$parts[1]))return true;elsereturn false;}elsereturn 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﹥ ﹤symbol﹥ben﹤/symbol﹥ ﹤code﹥a﹤/code﹥ ﹤/molecule﹥ ﹤molecule﹥ ﹤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 moleculeforeach ($xml-﹥molecule as $record){ //attribute are accessted by echo $record['name'], ' '; //node are accessted by -﹥ operator echo $record-﹥symbol, ' '; echo $record-﹥code, '﹤br /﹥';}
7、数据库连接
﹤?phpif(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") ordie ("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.'http://www.jb51.net/'.$file,$newlocation.'http://www.jb51.net/'.$file); unlink($location.'http://www.jb51.net/'.$file); } return true; }else{ return false; } }?﹥//use the code as following:﹤?phpinclude '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 '';echo strpos($str,'ph');
substr 截取字符串
$str = 'hello php';//截取字符串$str1 = substr($str,2,3); //从2位置开始截取,截取长度为3的字符串echo $str1;
不传入长度参数的话,会从指定位置一直截取到字符串的末尾
str_split 分割字符串 固定长度的分割(默认长度为1)
【相关教程推荐】
1. php编程从入门到精通全套视频教程
2. php从入门到精通
3. bootstrap教程
