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

首頁 > 編程 > PHP > 正文

php:總結(jié)一些實(shí)用的自定義函數(shù)

2020-03-22 18:42:47
字體:
供稿:網(wǎng)友
雖然PHP自帶的函數(shù)庫很強(qiáng)大,但是在日常很多情況下,我們也還得自己寫自定義的函數(shù)去實(shí)現(xiàn)某些功能與需求。下面收集了一些比較實(shí)用的、解決一些常見需求的自定義函數(shù),比如將網(wǎng)址html' target='_blank'>字符串轉(zhuǎn)換成超級(jí)鏈接、列出目錄內(nèi)容、驗(yàn)證郵件地址等等

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

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

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生成一個(gè)隨機(jī)字符串

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

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 字符實(shí)體,以防止被垃圾郵件程序收集。

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 = '<、a href="ma';     $part2 = 'ilto:';     $part3 = '" '. $attrs .' >';     $part4 = '<、/a>';        $encoded = '<、script type="text/javascript">';     $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 .= '<、/script>';        return $encoded; }

4. PHP驗(yàn)證郵件地址

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

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列出目錄內(nèi)容

function list_files($dir){   if(is_dir($dir))   {       if($handle = opendir($dir))       {           while(($file = readdir($handle)) !== false)           {               if($file != "." && $file != ".." && $file != "Thumbs.db"){                                    echo '<、a target="_blank" href="'.$dir.$file.'">'.$file.'<、/a><、br>'."/n";                 }           }           closedir($handle);       }   }}

6. PHP銷毀目錄

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

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 數(shù)據(jù)

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

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

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

//xml string $xml_string=" <、users> <、user id='398'> <、name>Foo<、/name> <、email>foo@bar.com<、/name> <、/user> <、user id='867'> <、name>Foobar<、/name> <、email>foobar@foo.com<、/name> <、/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, '<、br />'; }

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

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

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

10. PHP獲取客戶端真實(shí) IP 地址

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

function getRealIpAddr(){   if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))   {       $ip=$_SERVER['HTTP_CLIENT_IP'];   }   elseif (!emptyempty($_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;}

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

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

function force_download($file){   if ((isset($file))&&(file_exists($file))) {       header("Content-length: ".filesize($file));       header('Content-Type: application/octet-stream');       header('Content-Disposition: attachment; filename="' . $file . '"');       readfile("$file");   }   else {       echo "No file selected";   }}

以上就是php:總結(jié)一些實(shí)用的自定義函數(shù)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注 其它相關(guān)文章!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 延川县| 故城县| 岳池县| 武功县| 锡林浩特市| 太保市| 保山市| 清涧县| 南充市| 深泽县| 奉节县| 治多县| 新疆| 姚安县| 高密市| 寻甸| 金堂县| 金坛市| 肥城市| 阳曲县| 平定县| 莫力| 木兰县| 灵宝市| 胶州市| 龙陵县| 隆德县| 平舆县| 庆阳市| 仁寿县| 昆明市| 五寨县| 浮梁县| 留坝县| 亳州市| 昭平县| 正安县| 铜梁县| 镇赉县| 满洲里市| 金秀|