日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
分享經(jīng)常用到的21個PHP函數(shù)代碼段(上)

下面介紹的是,在PHP開發(fā)中,經(jīng)常用到的21個函數(shù)代碼段,當(dāng)我們用到的時候,就可以直接用了。

創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計與策劃設(shè)計,陳巴爾虎網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:陳巴爾虎等地區(qū)。陳巴爾虎做網(wǎng)站價格咨詢:028-86922220

1. PHP可閱讀隨機(jī)字符串

此代碼將創(chuàng)建一個可閱讀的字符串,使其更接近詞典中的單詞,實用且具有密碼驗證功能。

 
 
 
  1. /**************
  2. *@length – length of random string (must be a multiple of 2)
  3. **************/
  4. function readable_random_string($length = 6){
  5. $conso=array(“b”,”c”,”d”,”f”,”g”,”h”,”j”,”k”,”l”,
  6. “m”,”n”,”p”,”r”,”s”,”t”,”v”,”w”,”x”,”y”,”z”);
  7. $vocal=array(“a”,”e”,”i”,”o”,”u”);
  8. $password=”";
  9. srand ((double)microtime()*1000000);
  10. $max = $length/2;
  11. for($i=1; $i<=$max; $i++)
  12. {
  13. $password.=$conso[rand(0,19)];
  14. $password.=$vocal[rand(0,4)];
  15. }
  16. return $password;
  17. }

2. PHP生成一個隨機(jī)字符串

如果不需要可閱讀的字符串,使用此函數(shù)替代,即可創(chuàng)建一個隨機(jī)字符串,作為用戶的隨機(jī)密碼等。

 
 
 
  1. /*************
  2. *@l – length of random string
  3. */
  4. function generate_rand($l){
  5. $c= “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″;
  6. srand((double)microtime()*1000000);
  7. for($i=0; $i<$l; $i++) {
  8. $rand.= $c[rand()%strlen($c)];
  9. }
  10. return $rand;
  11. }

3. PHP編碼電子郵件地址

使用此代碼,可以將任何電子郵件地址編碼為 html 字符實體,以防止被垃圾郵件程序收集。

 
 
 
  1. function encode_email($email=’info@domain.com’, $linkText=’Contact Us’,
  2. $attrs =’class=”emailencoder”‘ )
  3. {
  4. // remplazar aroba y puntos
  5. $email = str_replace(‘@’, ‘@’, $email);
  6. $email = str_replace(‘.’, ‘.’, $email);
  7. $email = str_split($email, 5);
  8. $linkText = str_replace(‘@’, ‘@’, $linkText);
  9. $linkText = str_replace(‘.’, ‘.’, $linkText);
  10. $linkText = str_split($linkText, 5);
  11. $part1 = ‘
  12. $part2 = ‘ilto:’;
  13. $part3 = ‘” ‘. $attrs .’ >’;
  14. $part4 = ‘’;
  15. $encoded = ‘’;
  16. return $encoded;
  17. }

4. PHP驗證郵件地址

電子郵件驗證也許是中最常用的網(wǎng)頁表單驗證,此代碼除了驗證電子郵件地址,也可以選擇檢查郵件域所屬 DNS 中的 MX 記錄,使郵件驗證功能更加強(qiáng)大。

 
 
 
  1. function is_valid_email($email, $test_mx = false)
  2. {
  3. if(eregi(“^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$”, $email))
  4. if($test_mx)
  5. {
  6. list($username, $domain) = split(“@”, $email);
  7. return getmxrr($domain, $mxrecords);
  8. }
  9. else
  10. return true;
  11. else
  12. return false;
  13. }

5. PHP列出目錄內(nèi)容

 
 
 
  1. function list_files($dir)
  2. {
  3. if(is_dir($dir))
  4. {
  5. if($handle = opendir($dir))
  6. {
  7. while(($file = readdir($handle)) !== false)
  8. {
  9. if($file != “.” && $file != “..” && $file != “Thumbs.db”)
  10. {
  11. echo ‘’.$file.’
    ’.”\n”;
  12. }
  13. }
  14. closedir($handle);
  15. }
  16. }
  17. }

#p#

6. PHP銷毀目錄

刪除一個目錄,包括它的內(nèi)容。

 
 
 
  1. /*****
  2. *@dir – Directory to destroy
  3. *@virtual[optional]- whether a virtual directory
  4. */
  5. function destroyDir($dir, $virtual = false)
  6. {
  7. $ds = DIRECTORY_SEPARATOR;
  8. $dir = $virtual ? realpath($dir) : $dir;
  9. $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;
  10. if (is_dir($dir) && $handle = opendir($dir))
  11. {
  12. while ($file = readdir($handle))
  13. {
  14. if ($file == ‘.’ || $file == ‘..’)
  15. {
  16. continue;
  17. }
  18. elseif (is_dir($dir.$ds.$file))
  19. {
  20. destroyDir($dir.$ds.$file);
  21. }
  22. else
  23. {
  24. unlink($dir.$ds.$file);
  25. }
  26. }
  27. closedir($handle);
  28. rmdir($dir);
  29. return true;
  30. }
  31. else
  32. {
  33. return false;
  34. }
  35. }

7. PHP解析 JSON 數(shù)據(jù)

與大多數(shù)流行的 Web 服務(wù)如 twitter 通過開放 API 來提供數(shù)據(jù)一樣,它總是能夠知道如何解析 API 數(shù)據(jù)的各種傳送格式,包括 JSON,XML 等等。

 
 
 
  1. $json_string=’{“id”:1,”name”:”foo”,”email”:”foo@foobar.com”,”interest”:["wordpress","php"]} ‘;
  2. $obj=json_decode($json_string);
  3. echo $obj->name; //prints foo
  4. echo $obj->interest[1]; //prints php

8. PHP解析 XML 數(shù)據(jù)

 
 
 
  1. //xml string
  2. $xml_string=”
  3. Foo
  4. foo@bar.com
  5. Foobar
  6. foobar@foo.com
  7. ”;
  8. //load the xml string using simplexml
  9. $xml = simplexml_load_string($xml_string);
  10. //loop through the each node of user
  11. foreach ($xml->user as $user)
  12. {
  13. //access attribute
  14. echo $user['id'], ‘ ‘;
  15. //subnodes are accessed by -> operator
  16. echo $user->name, ‘ ‘;
  17. echo $user->email, ‘’;
  18. }

9. PHP創(chuàng)建日志縮略名

創(chuàng)建用戶友好的日志縮略名。

 
 
 
  1. function create_slug($string){
  2. $slug=preg_replace(‘/[^A-Za-z0-9-]+/’, ‘-’, $string);
  3. return $slug;
  4. }

10. PHP獲取客戶端真實 IP 地址

該函數(shù)將獲取用戶的真實 IP 地址,即便他使用代理服務(wù)器。

 
 
 
  1. function getRealIpAddr()
  2. {
  3. if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))
  4. {
  5. $ip=$_SERVER['HTTP_CLIENT_IP'];
  6. }
  7. elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR']))
  8. //to check ip is pass from proxy
  9. {
  10. $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  11. }
  12. else
  13. {
  14. $ip=$_SERVER['REMOTE_ADDR'];
  15. }
  16. return $ip;
  17. }

11. PHP強(qiáng)制性文件下載

為用戶提供強(qiáng)制性的文件下載功能。

 
 
 
  1. /********************
  2. *@file – path to file
  3. */
  4. function force_download($file)
  5. {
  6. if ((isset($file))&&(file_exists($file))) {
  7. header(“Content-length: “.filesize($file));
  8. header(‘Content-Type: application/octet-stream’);
  9. header(‘Content-Disposition: attachment; filename=”‘ . $file . ‘”‘);
  10. readfile(“$file”);
  11. } else {
  12. echo “No file selected”;
  13. }
  14. }

經(jīng)常用到的函數(shù)段,請看下一篇,


文章名稱:分享經(jīng)常用到的21個PHP函數(shù)代碼段(上)
URL鏈接:http://m.5511xx.com/article/ccdhdsj.html