麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PHP > 正文

分享10段PHP常用代碼

2024-05-04 23:40:18
字體:
來源:轉載
供稿:網友

 

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

1、使用PHP Mail函數發送Email

 

 
  1. $to = "[email protected]";  
  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: [email protected]/r/n";  
  6. $headers .= "Return-Path: [email protected]/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、獲取遠程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、驗證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、數據庫連接

 

 
  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、創建和解析JSON數據

 

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

9、處理MySQL時間戳

 

 
  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字符串

字符串聲明 變量=''或者""(一般情況會使用單引號,因為寫起來會比較方便)

$str = 'Hello PHP';

echo $str;

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

$str = 'Hello PHP';

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

echo '
';

echo strpos($str,'PH');

substr 截取字符串

 

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

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

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

 

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

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

 

 
  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; 


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九一免费在线观看 | 亚洲一区二区 | 97久久精品一区二区三区观看 | 欧美成人免费看 | 精品国产一区二区三区久久久蜜月 | 一级一级一级一级毛片 | 免费网站看毛片 | 日本精品久久久久久草草 | 久久久久久久久久久久久久国产 | 在线免费观看欧美 | 国产成人高清成人av片在线看 | 午夜生活理论片 | 精品国产一区二区三区免费 | 欧美精品一区自拍a毛片在线视频 | 国产精品av久久久久久久久久 | 国产在线地址 | 国产91av视频 | 久色porn | 欧美一级黑人 | 欧美无极品 | 亚洲国产高清一区 | 国产午夜精品久久久久久免费视 | 国产一级一片免费播放 | 国产1区2| 久久九九热re6这里有精品 | 日本成年免费网站 | av在线免费看片 | 亚洲视频高清 | 久久精热 | 香蕉久久久精品 | 精品无吗乱吗av国产爱色 | 国产chinesehd精品91 | 韩国精品久久久 | 久久国产一级片 | 男人的天堂毛片 | 91久久夜色精品国产网站 | 中文字幕网站在线 | 国产88久久久国产精品免费二区 | 亚洲va国产va | 成人在线视频免费播放 | 3344永久免费 |