1. PHP可阅读文章随机字符串
  此编码将建立一个可阅读文章的字符串数组,使其更贴近字典中的英语单词,好用且具备登陆密码认证作用。
	-  
 
	-  
 
	-  
 
	- 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转化成一个随机字符串
  假如不用可阅读文章的字符串数组,应用此涵数取代,就可以建立一个随机字符串,做为客户的随机密码等。
	-  
 
	-  
 
	-  
 
	- 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"' ) 
 
	- { 
 
	-      
 
	-     $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认证邮箱地址
  电子邮箱认证或许是中最常见的网页页面表单验证,此编码除开认证电子邮箱详细地址,还可以挑选查验电子邮件域隶属 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 '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."n"; 
 
	-                 } 
 
	-             } 
 
	-             closedir($handle); 
 
	-         } 
 
	-     } 
 
	- } 
 
6. PHP消毁文件目录
  删掉一个文件目录,包含它的內容。
	-  
 
	-  
 
	-  
 
	-  
 
	- 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&
nbsp;== '..') 
 
	-             { 
 
	-                 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;  
 
	- echo $obj->interest[1];  
 
8. PHP分析 XML 数据信息
	-  
 
	- $xml_string="<?xml version='1.0'?> 
 
	- <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>";  
 
	-  
 
	-  
 
	- $xml = simplexml_load_string($xml_string);  
 
	-  
 
	-  
 
	- foreach ($xml->user as $user) 
 
	- { 
 
	-  
 
	- echo $user['id'], ' '; 
 
	-  
 
	- echo $user->name, ' '; 
 
	- echo $user->email, '<br />'; 
 
	- } 
 
9. PHP建立系统日志缩略名
  建立客户友善的系统日志缩略名。
	- function create_slug($string){ 
 
	- $slug=preg_replace('/[^A-Za-z0-9-] /', '-', $string); 
 
	- return $slug; 
 
	- } 
 
10. PHP获得手机客户端真正 IP 详细地址
  该涵数将获得客户的真正 IP 详细地址,就算他应用服务器代理。
	- function getRealIpAddr() 
 
	- { 
 
	-     if (!emptyempty($_SERVER['HTTP_CLIE
NT_IP'])) 
 
	-     { 
 
	-         $ip=$_SERVER['HTTP_CLIENT_IP']; 
 
	-     } 
 
	-     elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
 
	-      
 
	-     { 
 
	-         $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
 
	-     } 
 
	-     else 
 
	-     { 
 
	-         $ip=$_SERVER['REMOTE_ADDR']; 
 
	-     } 
 
	-     return $ip; 
 
	- } 
 
11. PHP强制压缩文件下载
  为客户出示强制的压缩文件下载作用。
	-  
 
	-  
 
	-  
 
	- 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"; 
 
	- } 
 
	- } 
 
12. PHP建立标签云
	- function getCloud( $data = array(),&
nbsp;$minFontSize = 12, $maxFontSize = 30 ) 
 
	- { 
 
	- $minimumCount = min( array_values( $data ) ); 
 
	- $maximumCount = max( array_values( $data ) ); 
 
	- $spread = $maximumCount - $minimumCount; 
 
	- $cloudHTML = ''; 
 
	- $cloudTags = array();  
 
	-  
 
	- $spread == 0 && $spread = 1;  
 
	-  
 
	- foreach( $data as $tag => $count ) 
 
	- { 
 
	- $size = $minFontSize   ( $count - $minimumCount ) 
 
	- * ( $maxFontSize - $minFontSize ) / $spread; 
 
	- $cloudTags[] = '<a style="font-size: ' . floor( $size ) . 'px' 
 
	- . '" href="#" title="'' . $tag . 
 
	- '' returned a count of ' . $count . '">' 
 
	- . htmlspecialchars( stripslashes( $tag ) ) . '</a>'; 
 
	- }  
 
	-  
 
	- return join( "n", $cloudTags ) . "n"; 
 
	- } 
 
	-  
 
	-  
 
	- $arr = Array('Actionscript' => 35, 'Adobe' => 22, 'Array' => 44, 'Background' => 43, 
 
	- 'Blur' => 18, 'Canvas' => 33, 'Class' => 15, 'Color Palette' => 11, 'Crop' => 42, 
 
	- 'Delimiter' => 13, 'Depth' => 34, 'Design' => 8, 'Encode' => 12, 'Encryption' => 30, 
 
	- 'Extract' => 28, 'Filters' => 42); 
 
	- echo getCloud($arr, 12, 36); 
 
13. PHP找寻2个字符串数组的相似度
  PHP 出示了一个非常少应用的 similar_text 涵数,但此涵数十分有效,用以较为2个字符串数组并回到类似水平的百分数。
	- similar_text($string1, $string2, $percent); 
 
	-  
 
14. PHP在程序运行中应用 Gravatar 通用性头像图片
  伴随着 WordPress 愈来愈普及化,Gravatar 也随着时兴。因为 Gravatar 出示了便于应用的 API,将其列入程序运行也越来越十分便捷。
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function show_gravatar($email, $size, $default, $rating) 
 
	- { 
 
	- echo '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email). 
 
	- '&default='.$default.'&size='.$size.'&rating='.$rating.'" width="'.$size.'px" 
 
	- height="'.$size.'px" />'; 
 
	- } 
 
15. PHP在标识符中断点处断开文本
  说白了断字 (word break),即一个英语单词可在改行时断掉的地区。这一涵数将在断字处断开字符串数组。
	-  
 
	-  
 
	- function myTruncate($string, $limit, $break=".", $pad="...") { 
 
	-  
 
	- if(strlen($string) <= $limit) 
 
	- return $string;  
 
	-  
 
	-  
 
	- if(false !== ($breakpoint = strpos($string, $break, $limit))) { 
 
	- if($breakpoint < strlen($string) - 1) { 
 
	- $string = substr($string, 0, $breakpoint) . $pad; 
 
	- } 
 
	- } 
 
	- return $string; 
 
	- } 
 
	-  
 
	- $short_string=myTruncate($long_string, 100, ' '); 
 
16. PHP文档 Zip 缩小
	-  
 
	- function create_zip($files = array(),
$destination = '',$overwrite = false) { 
 
	-  
 
	- if(file_exists($destination) && !$overwrite) { return false; } 
 
	-  
 
	- $valid_files = array(); 
 
	-  
 
	- if(is_array($files)) { 
 
	-  
 
	- foreach($files as $file) { 
 
	-  
 
	- if(file_exists($file)) { 
 
	- $valid_files[] = $file; 
 
	- } 
 
	- } 
 
	- } 
 
	-  
 
	- if(count($valid_files)) { 
 
	-  
 
	- $zip = new ZipArchive(); 
 
	- if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
 
	- return false; 
 
	- } 
 
	-  
 
	- foreach($valid_files as $file) { 
 
	- $zip->addFile($file,$file); 
 
	- } 
 
	-  
 
	-  
 
	-  
 
	-  
 
	- $zip->close();  
 
	-  
 
	-  
 
	- return file_exists($destination); 
 
	- } 
 
	- else 
 
	- { 
 
	- return false; 
 
	- } 
 
	- } 
 
	-  
 
	- $files=array('file1.jpg', 'file2.jpg', 'file3.gif'); 
 
	- create_zip($files, 'myzipfile.zip', true); 
 
17. PHP压缩包解压 Zip 文档
	-  
 
	-  
 
	-  
 
	-  
 
	- function unzip_file($file, $destination){ 
 
	-  
 
	- $zip = new ZipArchive() ; 
 
	-  
 
	- if ($zip->open($file) !== TRUE) { 
 
	- die (’Could not open archive’); 
 
	- } 
 
	-  
 
	- $zip->extractTo($destination); 
 
	-  
 
	- $zip->close(); 
 
	- echo 'Archive extracted to directory'; 
 
	- } 
 
18. PHP为 URL 详细地址预置 http 字符串数组
  有时候必须接纳一些表格中的网站地址
键入,但客户非常少加上 http:// 字段名,此编码将为网站地址加上该字段名。
	- if (!preg_match("/^(http|ftp):/", $_POST['url'])) { 
 
	-    $_POST['url'] = 'http://'.$_POST['url']; 
 
	- } 
 
19. PHP将网站地址字符串数组转化成超链接
  该涵数将 URL 和 E-mail 详细地址字符串数组变换为可点一下的超链接。
	- function makeClickableLinks($text) { 
 
	- $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_ .~#?&//=] )', 
 
	- '<a href="1">1</a>', $text); 
 
	- $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_ .~#?&//=] )', 
 
	- '1<a href="http://2">2</a>', $text); 
 
	- $text = eregi_replace('([_.0-9a-z-] @([0-9a-z][0-9a-z-] .) [a-z]{2,3})', 
 
	- '<a href="mailto:1">1</a>', $text);  
 
	-  
 
	- return $text; 
 
	- } 
 
20. PHP调节图像尺寸
  建立图象缩列图必须很多時间,此编码将有利于掌握缩列图的逻辑性。
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	- function resize_image($filename, $tmpname, $xmax, $ymax) 
 
	- { 
 
	-     $ext = explode(".", $filename); 
 
	-     $ext = $ext[<
span class="func">count($ext)-1];   
 
	-  
 
	-     if($ext == "jpg" || $ext == "jpeg") 
 
	-         $im = imagecreatefromjpeg($tmpname); 
 
	-     elseif($ext == "png") 
 
	-         $im = imagecreatefrompng($tmpname); 
 
	-     elseif($ext == "gif") 
 
	-         $im = imagecreatefromgif($tmpname);   
 
	-  
 
	-     $x = imagesx($im); 
 
	-     $y = imagesy($im);   
 
	-  
 
	-     if($x <= $xmax && $y <= $ymax) 
 
	-         return $im;   
 
	-  
 
	-     if($x >= $y) { 
 
	-         $newx = $xmax; 
 
	-         $newy = $newx * $y / $x; 
 
	-     } 
 
	-     else { 
 
	-         $newy = $ymax; 
 
	-         $newx = $x / $y * $newy; 
 
	-     }   
 
	- <
span> 
 
	-     $im2 = imagecreatetruecolor($newx, $newy); 
 
	-     imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y); 
 
	-     return $im2; 
 
	- } 
 
21. PHP检验 ajax 要求
大部分的 JavaScript 架构如 jquery,Mootools 等,在传出 Ajax 要求时,都是会推送附加的 HTTP_X_REQUESTED_WITH 头顶部信息内容,头当她们一个ajax要求,因而你能在服务端探测到 Ajax 要求。
	- if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ 
 
	-      
 
	- }else
					来源于申明:以上内容一部分(包括照片、文本)来自互联网,若有侵权行为,请立即与本网站联络(010-57218159)。
						
						如没特殊注明,文章均为酷站科技原创,转载请注明来自http://bjkuzhan.com/jianzhanzhishi/3339.html						
                    
                    
                    
                        
                        联系专业的商务顾问,制定方案,专业设计,一对一咨询及其报价详情
                        
                            
服务热线
                            18911184380