QQ咨询 官方微信

添加微信好友

官方小程序

百度小程序

微信小程序

头条小程序

关于我们

PHP基础

开发者必备,超实用的PHP代码片段!

 admin  2013-11-23 16:24:00

    此前,研发频道曾发布《直接拿来用,10个PHP代码片段》,得到了网友们的一致好评。本文,笔者将继续分享九个超级有用的PHP代码片段。当你在开发网站、应用或者博客时,利用这些代码能为你节省大量的时间。


   一、查看邮件是否已被阅读

    当你在发送邮件时,你或许很想知道该邮件是否被对方已阅读。这里有段非常有趣的代码片段能够显示对方IP地址记录阅读的实际日期和时间。

  1. <? 
  2. 02. 
  3. error_reporting(0); 
  4. 03. 
  5. Header("Content-Type: image/jpeg"); 
  6. 04. 
  7.   
  8. 05. 
  9. //Get IP 
  10. 06. 
  11. if (!emptyempty($_SERVER['HTTP_CLIENT_IP'])) 
  12. 07. 
  13. 08. 
  14. $ip=$_SERVER['HTTP_CLIENT_IP']; 
  15. 09. 
  16. 10. 
  17. elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
  18. 11. 
  19. 12. 
  20. $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
  21. 13. 
  22. 14. 
  23. else 
  24. 15. 
  25. 16. 
  26. $ip=$_SERVER['REMOTE_ADDR']; 
  27. 17. 
  28. 18. 
  29.   
  30. 19. 
  31. //Time 
  32. 20. 
  33. $actual_time = time(); 
  34. 21. 
  35. $actual_day = date('Y.m.d', $actual_time); 
  36. 22. 
  37. $actual_day_chart = date('d/m/y', $actual_time); 
  38. 23. 
  39. $actual_hour = date('H:i:s', $actual_time); 
  40. 24. 
  41.   
  42. 25. 
  43. //GET Browser 
  44. 26. 
  45. $browser = $_SERVER['HTTP_USER_AGENT']; 
  46. 27. 
  47.   
  48. 28. 
  49. //LOG 
  50. 29. 
  51. $myFile = "log.txt"
  52. 30. 
  53. $fh = fopen($myFile, 'a+'); 
  54. 31. 
  55. $stringData = $actual_day . ' ' . $actual_hour . ' ' . $ip . ' ' . $browser . ' ' . "rn"; 
  56. 32. 
  57. fwrite($fh, $stringData); 
  58. 33. 
  59. fclose($fh); 
  60. 34. 
  61.   
  62. 35. 
  63. //Generate Image (Es. dimesion is 1x1) 
  64. 36. 
  65. $newimage = ImageCreate(1,1); 
  66. 37. 
  67. $grigio = ImageColorAllocate($newimage,255,255,255); 
  68. 38. 
  69. ImageJPEG($newimage); 
  70. 39. 
  71. ImageDestroy($newimage); 
  72. 40. 
  73.   
  74. 41. 
  75. ?> 

二、从网页中提取关键字

一段伟大的代码片段能够轻松的从网页中提取关键字。

  1. $meta = get_meta_tags('http://www.emoticode.net/'); 
  2. 02. 
  3. $keywords = $meta['keywords']; 
  4. 03. 
  5. // Split keywords 
  6. 04. 
  7. $keywords = explode(',', $keywords ); 
  8. 05. 
  9. // Trim them 
  10. 06. 
  11. $keywords = array_map( 'trim', $keywords ); 
  12. 07. 
  13. // Remove empty values 
  14. 08. 
  15. $keywords = array_filter( $keywords ); 
  16. 09. 
  17.   
  18. 10. 
  19. print_r( $keywords ); 

三、查找页面上的所有链接
使用DOM,你可以轻松从任何页面上抓取链接,代码示例如下:

  1. $html = file_get_contents('http://www.php100.com'); 
  2. 02. 
  3.   
  4. 03. 
  5. $dom = new DOMDocument(); 
  6. 04. 
  7. @$dom->loadHTML($html); 
  8. 05. 
  9.   
  10. 06. 
  11. // grab all the on the page 
  12. 07. 
  13. $xpath = new DOMXPath($dom); 
  14. 08. 
  15. $hrefs = $xpath->evaluate("/html/body//a"); 
  16. 09. 
  17.   
  18. 10. 
  19. for ($i = 0; $i < $hrefs->length; $i++) { 
  20. 11. 
  21. $href = $hrefs->item($i); 
  22. 12. 
  23. $url = $href->getAttribute('href'); 
  24. 13. 
  25. echo $url.'<br />'; 

四、自动转换URL,跳转至超链接
在WordPress中,如果你想自动转换URL,跳转至超链接页面,你可以利用内置的函数make_clickable()执行此操作。如果你想基于WordPress之外操作该程序,那么你可以参考wp-includes/formatting.php源代码。

  1. function _make_url_clickable_cb($matches) { 
  2. 02. 
  3. $ret = ''
  4. 03. 
  5. $url = $matches[2]; 
  6. 04. 
  7.   
  8. 05. 
  9. if ( emptyempty($url) ) 
  10. 06. 
  11. return $matches[0]; 
  12. 07. 
  13. // removed trailing [.,;:] from URL 
  14. 08. 
  15. if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) { 
  16. 09. 
  17. $ret = substr($url, -1); 
  18. 10. 
  19. $url = substr($url, 0, strlen($url)-1); 
  20. 11. 
  21. 12. 
  22. return $matches[1] . "<a href="$url" rel="nofollow">$url</a>" . $ret; 
  23. 13. 
  24. 14. 
  25.   
  26. 15. 
  27. function _make_web_ftp_clickable_cb($matches) { 
  28. 16. 
  29. $ret = ''
  30. 17. 
  31. $dest = $matches[2]; 
  32. 18. 
  33. $dest = 'http://' . $dest; 
  34. 19. 
  35.   
  36. 20. 
  37. if ( emptyempty($dest) ) 
  38. 21. 
  39. return $matches[0]; 
  40. 22. 
  41. // removed trailing [,;:] from URL 
  42. 23. 
  43. if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) { 
  44. 24. 
  45. $ret = substr($dest, -1); 
  46. 25. 
  47. $dest = substr($dest, 0, strlen($dest)-1); 
  48. 26. 
  49. 27. 
  50. return $matches[1] . "<a href="$dest" rel="nofollow">$dest</a>" . $ret; 
  51. 28. 
  52. 29. 
  53.   
  54. 30. 
  55. function _make_email_clickable_cb($matches) { 
  56. 31. 
  57. $email = $matches[2] . '@' . $matches[3]; 
  58. 32. 
  59. return $matches[1] . "<a href="mailto:$email">$email</a>"; 
  60. 33. 
  61. 34. 
  62.   
  63. 35. 
  64. function make_clickable($ret) { 
  65. 36. 
  66. $ret = ' ' . $ret; 
  67. 37. 
  68. // in testing, using arrays here was found to be faster 
  69. 38. 
  70. $ret = preg_replace_callback('#([s>])([w]+?://[w\x80-\xff#$%&~/.-;:=,?@[]+]*)#is', '_make_url_clickable_cb', $ret); 
  71. 39. 
  72. $ret = preg_replace_callback('#([s>])((www|ftp).[w\x80-\xff#$%&~/.-;:=,?@[]+]*)#is', '_make_web_ftp_clickable_cb', $ret); 
  73. 40. 
  74. $ret = preg_replace_callback('#([s>])([.0-9a-z_+-]+)@(([0-9a-z-]+.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret); 
  75. 41. 
  76.   
  77. 42. 
  78. // this one is not in an array because we need it to run last, for cleanup of accidental links within links 
  79. 43. 
  80. $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret); 
  81. 44. 
  82. $ret = trim($ret); 
  83. 45. 
  84. return $ret; 
  85. 46. 

五、创建数据URL
数据URL可以直接嵌入到HTML/CSS/JS中,以节省大量的 HTTP请求。 下面的这段代码可利用$file轻松创建数据URL。

  1. function data_uri($file, $mime) { 
  2. 2. 
  3. $contents=file_get_contents($file); 
  4. 3. 
  5. $base64=base64_encode($contents); 
  6. 4. 
  7. echo "data:$mime;base64,$base64"; 
  8. 5. 

六、从服务器上下载&保存一个远程图片
当你在搭建网站时,从远程服务器下载某张图片并且将其保存在自己的服务器上,这一操作会经常用到。代码如下:

  1. $image = file_get_contents('http://www.php100.com/image.jpg'); 
  2. 2. 
  3. file_put_contents('/images/image.jpg', $image);//Where to save the image 

七、移除Remove Microsoft Word HTML Tag
    当你使用Microsoft Word会创建许多Tag,比如font,span,style,class等。这些标签对于Word本身而言是非常有用的,但是当你从Word粘贴至网页时,你会发现很多无用的Tag。因此,下面的这段代码可帮助你删除所有无用的Word HTML Tag。

  1. function cleanHTML($html) { 
  2. 02. 
  3. /// <summary> 
  4. 03. 
  5. /// Removes all FONT and SPAN tags, and all Class and Style attributes. 
  6. 04. 
  7. /// Designed to get rid of non-standard Microsoft Word HTML tags. 
  8. 05. 
  9. /// </summary> 
  10. 06. 
  11. // start by completely removing all unwanted tags 
  12. 07. 
  13.   
  14. 08. 
  15. $html = ereg_replace("<(/)?(font|span|del|ins)[^>]*>","",$html); 
  16. 09. 
  17.   
  18. 10. 
  19. // then run another pass over the html (twice), removing unwanted attributes 
  20. 11. 
  21.   
  22. 12. 
  23. $html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<1>",$html); 
  24. 13. 
  25. $html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<1>",$html); 
  26. 14. 
  27.   
  28. 15. 
  29. return $html 
  30. 16. 

八、检测浏览器语言
    如果你的网站上有多种语言,那么可以使用这段代码作为默认的语言来检测浏览器语言。该段代码将返回浏览器客户端使用的初始语言。

  1. function get_client_language($availableLanguages, $default='en'){ 
  2. 02. 
  3. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { 
  4. 03. 
  5. $langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']); 
  6. 04. 
  7.   
  8. 05. 
  9. foreach ($langs as $value){ 
  10. 06. 
  11. $choice=substr($value,0,2); 
  12. 07. 
  13. if(in_array($choice, $availableLanguages)){ 
  14. 08. 
  15. return $choice; 
  16. 09. 
  17. 10. 
  18. 11. 
  19. 12. 
  20. return $default; 
  21. 13. 

九、显示Facebook 粉丝数量
如果你的网站或者博客上有内链的Facebook页面,你或许想知道拥有多少粉丝。这段代码将帮助你查看Facebook粉丝数,记住,别忘了在你的页面ID第二行添加该段代码。

  1. ?php 
  2. 2. 
  3. $page_id = "YOUR PAGE-ID"
  4. 3. 
  5. $xml = @simplexml_load_file("
  6. http://api.facebook.com/restserver.php?method=facebook.fql.query&;
  7. query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") 
  8. or die ("a lot"); 
  9. 4. 
  10. $fans = $xml->page->fan_count; 
  11. 5. 
  12. echo $fans; 
  13. 6. 
  14. ?> 

¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《开发者必备,超实用的PHP代码片段!》发布于石头博客文章,作者:admin,如若转载,请注明出处:https://www.pweb123.com/html/php/181.html,否则禁止转载,谢谢配合!

文章点评

我来说两句 已有0条评论
点击图片更换

添加微信好友

添加微信好友

微信小程序

百度小程序