国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

分享10段PHP常用代碼

2024-05-04 23:40:18
字體:
供稿:網(wǎng)友

 

本文匯集PHP開發(fā)中經(jīng)常用到的十段代碼,包括Email、64位編碼和解碼、解壓縮、64位編碼、解析JSON等,希望對您有所幫助。

1、使用PHP Mail函數(shù)發(fā)送Email

 

 
  1. $to = "viralpatel.net@gmail.com";  
  2. $subject = "VIRALPATEL.net";  
  3. $body = "Body of your message here you can use HTML too. e.g. ﹤br﹥ ﹤b﹥ Bold ﹤/b﹥";  
  4. $headers = "From: Peter/r/n";  
  5. $headers .= "Reply-To: info@yoursite.com/r/n";  
  6. $headers .= "Return-Path: info@yoursite.com/r/n";  
  7. $headers .= "X-Mailer: PHP5/n";  
  8. $headers .= 'MIME-Version: 1.0' . "/n";  
  9. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "/r/n";  
  10. mail($to,$subject,$body,$headers);  
  11. ?﹥ 

2、PHP中的64位編碼和解碼

 

 
  1. function base64url_encode($plainText) { 
  2. $base64 = base64_encode($plainText); 
  3. $base64url = strtr($base64, '+/=''-_,'); 
  4. return $base64url; 
  5. function base64url_decode($plainText) { 
  6. $base64url = strtr($plainText, '-_,''+/='); 
  7. $base64 = base64_decode($base64url); 
  8. return $base64; 

3、獲取遠(yuǎn)程IP地址

 

 
  1. function getRealIPAddr() 
  2. if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet 
  3. $ip=$_SERVER['HTTP_CLIENT_IP']; 
  4. elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy 
  5. $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
  6. else 
  7. $ip=$_SERVER['REMOTE_ADDR']; 
  8. return $ip; 

4、 日期格式化

 

 
  1. function checkDateFormat($date) 
  2. //match the format of the date 
  3. if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts)) 
  4. //check weather the date is valid of not 
  5. if(checkdate($parts[2],$parts[3],$parts[1])) 
  6. return true
  7. else 
  8. return false
  9. else 
  10. return false

5、驗(yàn)證Email

 

 
  1. $email = $_POST['email']; 
  2. if(preg_match("~([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~])@([a-zA-Z0-9-]). 
  3. ([a-zA-Z0-9]{2,4})~",$email)) { 
  4. echo 'This is a valid email.'
  5. else
  6. echo 'This is an invalid email.'

6、在PHP中輕松解析XML

 

 
  1. //this is a sample xml string 
  2. $xml_string="﹤?xml version='1.0'?﹥ 
  3. ﹤moleculedb﹥ 
  4. ﹤molecule name='Benzine'﹥ 
  5. ﹤symbol﹥ben﹤/symbol﹥ 
  6. ﹤code﹥A﹤/code﹥ 
  7. ﹤/molecule﹥ 
  8. ﹤molecule name='Water'﹥ 
  9. ﹤symbol﹥h2o﹤/symbol﹥ 
  10. ﹤code﹥K﹤/code﹥ 
  11. ﹤/molecule﹥ 
  12. ﹤/moleculedb﹥"; 
  13. //load the xml string using simplexml function 
  14. $xml = simplexml_load_string($xml_string); 
  15. //loop through the each node of molecule 
  16. foreach ($xml-﹥molecule as $record) 
  17. //attribute are accessted by 
  18. echo $record['name'], ' '
  19. //node are accessted by -﹥ operator 
  20. echo $record-﹥symbol, ' '
  21. echo $record-﹥code, '﹤br /﹥'

7、數(shù)據(jù)庫連接

 

 
  1. ﹤?php 
  2. if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404(); 
  3. $dbHost = "localhost"//Location Of Database usually its localhost 
  4. $dbUser = "xxxx"//Database User Name 
  5. $dbPass = "xxxx"//Database Password 
  6. $dbDatabase = "xxxx"//Database Name 
  7. $db = mysql_connect("$dbHost""$dbUser""$dbPass") or 
  8. die ("Error connecting to database."); 
  9. mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database."); 
  10. # This function will send an imitation 404 page if the user 
  11. # types in this files filename into the address bar. 
  12. # only files connecting with in the same directory as this 
  13. # file will be able to use it as well. 
  14. function send_404() 
  15. header('HTTP/1.x 404 Not Found'); 
  16. print '﹤!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"﹥'."n"
  17. '﹤html﹥﹤head﹥'."n"
  18. '﹤title﹥404 Not Found﹤/title﹥'."n"
  19. '﹤/head﹥﹤body﹥'."n"
  20. '﹤h1﹥Not Found﹤/h1﹥'."n"
  21. '﹤p﹥The requested URL '
  22. str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']). 
  23. ' was not found on this server.﹤/p﹥'."n"
  24. '﹤/body﹥﹤/html﹥'."n"
  25. exit; 
  26. # In any file you want to connect to the database, 
  27. # and in this case we will name this file db.php 
  28. # just add this line of php code (without the pound sign): 
  29. # include"db.php"; 
  30. ?﹥ 

8、創(chuàng)建和解析JSON數(shù)據(jù)

 

 
  1. $json_data = array ('id'=﹥1,'name'=﹥"rolf",'country'=﹥'russia'
  2. "office"=﹥array("google","oracle")); 
  3. echo json_encode($json_data); 

9、處理MySQL時(shí)間戳

 

 
  1. $query = "select UNIX_TIMESTAMP(date_field) as mydate  
  2. from mytable where 1=1"; 
  3. $records = mysql_query($query) or die(mysql_error()); 
  4. while($row = mysql_fetch_array($records)) 
  5. echo $row; 

10、解壓縮Zip文件

 

 
  1. ﹤?php 
  2. function unzip($location,$newLocation){ 
  3. if(exec("unzip $location",$arr)){ 
  4. mkdir($newLocation); 
  5. for($i = 1;$i﹤ count($arr);$i++){ 
  6. $file = trim(preg_replace("~inflating: ~","",$arr[$i])); 
  7. copy($location.'/'.$file,$newLocation.'/'.$file); 
  8. unlink($location.'/'.$file); 
  9. return TRUE; 
  10. }else
  11. return FALSE; 
  12. ?﹥ 
  13. //Use the code as following: 
  14. ﹤?php 
  15. include 'functions.php'
  16. if(unzip('zipedfiles/test.zip','unziped/myNewZip')) 
  17. echo 'Success!'
  18. else 
  19. echo 'Error'
  20. ?﹥ 

PHP常用功能如下

1.PHP字符串

字符串聲明 變量=''或者""(一般情況會(huì)使用單引號,因?yàn)閷懫饋頃?huì)比較方便)

$str = 'Hello PHP';

echo $str;

strpos 計(jì)算字符在字符串中的位置(從0開始)

$str = 'Hello PHP';

echo strpos($str,'o'); //計(jì)算字符在字符串中的位置

echo '
';

echo strpos($str,'PH');

substr 截取字符串

 

 
  1. $str = 'Hello PHP'
  2. //截取字符串 
  3. $str1 = substr($str,2,3); //從2位置開始截取,截取長度為3的字符串 
  4. echo $str1; 

不傳入長度參數(shù)的話,會(huì)從指定位置一直截取到字符串的末尾

str_split 分割字符串 固定長度的分割(默認(rèn)長度為1)

 

 
  1. $str = 'Hello PHP'
  2. //分割字符串 
  3. $result = str_split($str); //將結(jié)果保存到一個(gè)數(shù)組中 
  4. print_r($result); //使用print_r輸入一個(gè)數(shù)組 
  5. echo '<br/>'
  6. $result1 = str_split($str,2); 
  7. print_r($result1); 

explode(分割字符,待分割的字符串) 按照空格進(jìn)行分割

 

 
  1. $str = 'Hello PHP Java C# C++'
  2. $result = explode(' ',$str); 
  3. print_r($result); 

字符串的連接

 

 
  1. $str = 'Hello PHP Java C# C++'
  2. //字符串的連接 
  3. $num = 100; 
  4. $str1 = $str.'<br/>Objective-C '.$num; 
  5. echo $str1; 
  6. echo '<br/>'
  7. $str2 = "$str<br/>Objective-C $num"//另一中簡便的寫法 
  8. echo $str2; 


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永登县| 土默特左旗| 手游| 盐城市| 贵南县| 雷州市| 昌宁县| 奉新县| 莒南县| 伊宁市| 丽江市| 渝北区| 正安县| 额尔古纳市| 诸暨市| 平遥县| 延庆县| 亚东县| 蓬莱市| 崇左市| 林甸县| 水城县| 汾阳市| 兴安县| 翁牛特旗| 阳山县| 大方县| 衡水市| 安达市| 丘北县| 墨竹工卡县| 文登市| 定安县| 江油市| 克什克腾旗| 景谷| 益阳市| 调兵山市| 拜城县| 望江县| 和田市|