php 抓取网页生成图片(4.PHP验证邮件地址电子邮件地址使用目录删除一个目录的内容)
优采云 发布时间: 2022-03-18 15:14php 抓取网页生成图片(4.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; $iname; //prints foo echo $obj->interest[1]; //prints php
8. PHP 解析 XML 数据
//xml string $xml_string=" 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, ''; }
#p#
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_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 强制文件下载
为用户提供强制文件下载功能。
/******************** *@file - path to file */ 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(), $minFontSize = 12, $maxFontSize = 30 ) { $minminimumCount = min( array_values( $data ) ); $maxmaximumCount = 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[] = '' . htmlspecialchars( stripslashes( $tag ) ) . 'a>'; } return join( "\n", $cloudTags ) . "\n"; } /*************************** *** Sample usage ***/ $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 查找两个字符串的相似度
PHP 提供了一个很少使用的similar_text 函数,但是这个函数对于比较两个字符串并返回百分比相似度非常有用。
similar_text($string1, $string2, $percent); //$percent will have the percentage of similarity
14. PHP在应用中使用Gravatar通用头像
随着 WordPress 越来越受欢迎,Gravatar 也越来越受欢迎。由于 Gravatar 提供了易于使用的 API,因此将其集成到应用程序中也很容易。
/****************** *@email - Email address to show gravatar for *@size - size of gravatar *@default - URL of default gravatar to use *@rating - rating of Gravatar(G, PG, R, X) */ function show_gravatar($email, $size, $default, $rating) { echo ''; }
#p#
15. PHP 在字符断点处截断文本
断字是换行时可以断字的地方。此函数将在断字处截断字符串。
// Original PHP code by Chirp Internet: www.chirp.com.au // Please acknowledge use of this code by including this header. function myTruncate($string, $limit, $break=".", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) addFile($file,$file); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! $zip->close(); //check to make sure the file exists return file_exists($destination); } else { return false; } } /***** Example Usage ***/ $files=array('file1.jpg', 'file2.jpg', 'file3.gif'); create_zip($files, 'myzipfile.zip', true);
17. PHP 解压 Zip 文件
/********************** *@file - path to zip file *@destination - destination directory for unzipped files */ function unzip_file($file, $destination){ // create object $zip = new ZipArchive() ; // open archive if ($zip->open($file) !== TRUE) { die (’Could not open archive’); } // extract contents to destination directory $zip->extractTo($destination); // close archive $zip->close(); echo 'Archive extracted to directory'; }
#p#
18. PHP为URL地址预设http字符串
有时需要在某些表单中接受 url 输入,但用户很少添加字段,此代码将为 url 添加该字段。
if (!preg_match("/^(http|ftp):/", $_POST['url'])) { $_POST['url'] = 'http://'.$_POST['url']; }
19. PHP 将 URL 字符串转换为超链接
此函数将 URL 和电子邮件地址字符串转换为可点击的超链接。
function makeClickableLinks($text) { $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)', '\1a>', $text); $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)', '\1\2a>', $text); $text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})', '\1a>', $text); return $text; }
20. PHP 调整图片大小
创建图像缩略图需要很多时间,这段代码将有助于理解缩略图的逻辑。
<p>/********************** *@filename - path to the image *@tmpname - temporary path to thumbnail *@xmax - max width *@ymax - max height */ function resize_image($filename, $tmpname, $xmax, $ymax) { $ext = explode(".", $filename); $ext = $ext[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