PHP中一些常用操作類別程式碼解析

coldplay.xixi
發布: 2023-04-08 20:48:02
轉載
2589 人瀏覽過

PHP中一些常用操作類別程式碼解析

PHP中一些常用操作類別程式碼範例一

1. PHP可閱讀隨機字串

此程式碼將建立一個可閱讀的字串,使其更接近字典中的單字,實用且具

/**@length - length of random string (must be a multiple of 2)**/
  function readable_random_string($length = 6){
      $conso=array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z");
      $vocal=array("a","e","i","o","u");
      $password="";
      srand ((double)microtime()*1000000);
      $max = $length/2;
      for($i=1;$i<=$max; $i++){
          $password.=$conso[rand(0,19)];
          $password.=$vocal[rand(0,4)];
      }
      return $password;
  }
登入後複製

 有密碼驗證功能。

2. PHP產生一個隨機字串

如果不需要可閱讀的字串,使用此函數替代,即可建立一個隨機字串,作為使用者的隨機密碼等。

/************* *@l - length of random string */ 
function generate_rand($l){ 
    $c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 
    srand((double)microtime()*1000000); 
    for($i=0; $i<$l; $i++) { 
        $rand.= 
    $c[rand()%strlen($c)]; 
    }
    return $rand; 
}
登入後複製

3. PHP編碼電子郵件地址

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

function encode_email($email='info@domain.com', $linkText='Contact Us',$attrs ='class="emailencoder"' ) { 
    // remplazar aroba y puntos $email = 
str_replace('@', '@', $email); 
    $email = str_replace('.', '.', $email); 
    $email = 
str_split($email, 5); 
    $linkText = str_replace('@', '@', $linkText); 
    $linkText = 
str_replace('.', '.', $linkText); 
    $linkText = str_split($linkText, 5); 
    $part1 = 'part2 = 'ilto:'; 
    $part3 = '" '. $attrs .' >'; 
    $part4 = ''; $encoded = ''; 
$encoded .= "document.write('$part1');"; 
    $encoded .= "document.write('$part2');"; 
    foreach($email as $e) { 
        $encoded .= "document.write('$e');"; 
    } 
    $encoded .= "document.write('$part3');"; 
foreach($linkText as $l) { 
        $encoded .= "document.write('$l');"; 
    } 
    $encoded .= "document.write('$part4');"; 
    $encoded .= ''; 
    return $encoded;
}
登入後複製

4. PHP驗證郵件地址

電子郵件驗證也許是中最常用的網頁表單驗證,此程式碼除了驗證電子郵件地址,也可以選擇檢查郵件網域所屬DNS 中的MX 記錄,讓郵件驗證功能更強大。

function is_valid_email($email, $test_mx = false) { 
if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) 
    if($test_mx) { 
        list($username, $domain) = split("@", $email); 
        return getmxrr($domain, $mxrecords); 
    } else {
        return true; 
    }
     
}  else {
    return false; 
}
登入後複製

5. PHP列出目錄內容  

function list_files($dir){ 
    if(is_dir($dir)) { 
        if($handle = opendir($dir)) { 
            while(($file = readdir($handle)) !== false) { 
                if($file != "." && $file != ".." && $file != "Thumbs.db") { 
                    echo ''.$file.'a>  '."\n"; 
                 } 
            } closedir($handle); 
        } 
   } 
}
登入後複製

6. PHP銷毀目錄

#刪除一個目錄,包括它的內容。

/***** *@dir - Directory to destroy *@virtual[optional]- whether a virtual directory */ 
function destroyDir($dir, $virtual = false) { 
    $ds = DIRECTORY_SEPARATOR; 
    $dir = $virtual ? realpath($dir) : $dir; 
    $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir; 
    if (is_dir($dir) && $handle = opendir($dir)) { 
        while ($file = readdir($handle)) { 
            if ($file == '.' || $file == '..') { 
                continue; 
            } elseif (is_dir($dir.$ds.$file)) { 
                destroyDir($dir.$ds.$file); 
            } else { 
                unlink($dir.$ds.$file); 
            } 
        } 
        closedir($handle); 
        rmdir($dir); 
        return true; 
    } else { 
        return false; 
    } 
}
登入後複製

7. PHP解析JSON 資料

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

$json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} '; 
$obj=json_decode($json_string); 
echo $obj->name; //prints foo echo 
$obj->interest[1]; //prints php
登入後複製

8. PHP解析 XML 資料

//xml string $xml_string="xml version='1.0'?> Fooname> foo@bar.comname> user> Foobarname> foobar@foo.comname> user>users>"; 
 
//load the xml string using simplexml 
$xml = simplexml_load_string($xml_string); 
 
//loop through the each node of user 
foreach ($xml->user as $user) { 
    //access attribute 
    echo $user['id'], ' '; 
     
    //subnodes are accessed by -> operator 
    echo $user->name, ' '; 
    echo $user->email,''; 
}
登入後複製

9. PHP建立日誌縮寫

建立使用者友善的日誌縮寫。

function create_slug($string){ 
    $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string); 
   return $slug; 
}
登入後複製

推薦教學:《PHP影片教學

以上是PHP中一些常用操作類別程式碼解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:liqingbo.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!