php 抓取网页生成图片( php常用方法汇总,你知道几个?(上))

优采云 发布时间: 2022-03-22 00:24

  php 抓取网页生成图片(

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; $iinterest[1]; //prints php

  8. PHP 解析 XML 数据

  //xml string $xml_string=" Foo foo@bar.com Foobar foobar@foo.com "; //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[&#39;id&#39;], &#39; &#39;; //subnodes are accessed by -> operator echo $user->name, &#39; &#39;; echo $user->email, &#39;<br />&#39;; }

  9. PHP 创建日志缩写

  创建用户友好的日志缩写。

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

  10. PHP获取客户端的真实IP地址

  这个函数会得到用户的真实IP地址,即使他使用了代理服务器。

  function getRealIpAddr() { if (!emptyempty($_SERVER[&#39;HTTP_CLIENT_IP&#39;])) { $ip=$_SERVER[&#39;HTTP_CLIENT_IP&#39;]; } elseif (!emptyempty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])) //to check ip is pass from proxy { $ip=$_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]; } else { $ip=$_SERVER[&#39;REMOTE_ADDR&#39;]; } return $ip; }

  11. PHP 强制文件下载

  为用户提供强制文件下载功能。

  /******************** *@file - path to file */

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

  12. PHP 创建标签云

  function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 ) { $minimumCount = min( array_values( $data ) ); $maximumCount = max( array_values( $data ) ); $spread = $maximumCount - $minimumCount; $cloudHTML = &#39;&#39;; $cloudTags = array(); $spread == 0 && $spread = 1; foreach( $data as $tag => $count ) { $size = $minFontSize + ( $count - $minimumCount ) * ( $maxFontSize - $minFontSize ) / $spread; $cloudTags[] = &#39;&#39; . htmlspecialchars( stripslashes( $tag ) ) . &#39;&#39;; } return join( "\n", $cloudTags ) . "\n"; } /************************** **** Sample usage ***/ $arr = Array(&#39;Actionscript&#39; => 35, &#39;Adobe&#39; => 22, &#39;Array&#39; => 44, &#39;Background&#39; => 43, &#39;Blur&#39; => 18, &#39;Canvas&#39; => 33, &#39;Class&#39; => 15, &#39;Color Palette&#39; => 11, &#39;Crop&#39; => 42, &#39;Delimiter&#39; => 13, &#39;Depth&#39; => 34, &#39;Design&#39; => 8, &#39;Encode&#39; => 12, &#39;Encryption&#39; => 30, &#39;Extract&#39; => 28, &#39;Filters&#39; => 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 &#39;

&#39;; }

  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) open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo &#39;The zip archive contains &#39;,$zip->numFiles,&#39; files with a status of &#39;,$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(&#39;file1.jpg&#39;, &#39;file2.jpg&#39;, &#39;file3.gif&#39;); create_zip($files, &#39;myzipfile.zip&#39;, 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 &#39;Archive extracted to directory&#39;; }

  18. PHP为URL地址预设http字符串

  有时在某些表单中需要接受 url 输入,但用户很少添加字段,此代码将为 url 添加该字段。

  if (!preg_match("/^(http|ftp):/", $_POST[&#39;url&#39;])) { $_POST[&#39;url&#39;] = &#39;http://&#39;.$_POST[&#39;url&#39;]; }

  19. PHP 将 URL 字符串转换为超链接

  此函数将 URL 和电子邮件地址字符串转换为可点击的超链接。

  function makeClickableLinks($text) { $text = eregi_replace(&#39;(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)&#39;, &#39;\1&#39;, $text); $text = eregi_replace(&#39;([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)&#39;, &#39;\1\2&#39;, $text); $text = eregi_replace(&#39;([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})&#39;, &#39;\1&#39;, $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

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线