自定义数组格式化输出函数(dump),调试程序时很有用

前面我们谈了一下关于判断远程文件是否存在的一个函数,希望大家能够记下来,因为在最近这几篇文章中这几个函数会联合一起使用,将来这些函数也会被我使用到改写的MG2程序里。

下面说说今天要说的这个自定义函数dump,该函数是我从网上搜集下来的,作用为将一个数组进行格式化输出,对于我来说要看php中一个数组的内容是很费力的,因为本人的php可以说是无基础可言,用到哪里就看哪里,呵呵,完全的现用现学。搜到这个函数后,发现通过格式化输出后,再去看某一个数组时确实省事多了,经过测试,暂无发现什么问题,下面给出该函数代码:

  1. function dump($vars, $label = '', $return = false) {
  2.     if (ini_get('html_errors')) {
  3.         $content = "<pre>\n";
  4.         if ($label != '') {
  5.             $content .= "<strong>{$label} :</strong>\n";
  6.         }
  7.         $content .= htmlspecialchars(print_r($vars, true));
  8.         $content .= "\n</pre>\n";
  9.     } else {
  10.         $content = $label . " :\n" . print_r($vars, true);
  11.     }
  12.     if ($return) { return $content; }
  13.     echo $content;
  14.     return null;
  15. }

以上代码为原文,未经修改,可以正常使用,下面给出一个例程,和输出结果,通过这个函数,我们在写一些php代码时,在调试阶段就可以很清楚的查到数组内容了,例程中应用到了本函数及前面讲的判断远程文件是否存在的函数remote_file_exists以及get_headers函数,例程及输出结果如下:

  1. function dump($vars, $label = '', $return = false) {
  2.     if (ini_get('html_errors')) {
  3.         $content = "<pre>\n";
  4.         if ($label != '') {
  5.             $content .= "<strong>{$label} :</strong>\n";
  6.         }
  7.         $content .= htmlspecialchars(print_r($vars, true));
  8.         $content .= "\n</pre>\n";
  9.     } else {
  10.         $content = $label . " :\n" . print_r($vars, true);
  11.     }
  12.     if ($return) { return $content; }
  13.     echo $content;
  14.     return null;
  15. }
  16.   function remote_file_exists($url_file){
  17.     $url_file = trim($url_file);
  18.     if (empty($url_file)) return false;
  19.     $url_arr = parse_url($url_file);
  20.     if (!is_array($url_arr) || empty($url_arr)) return false;
  21.     $host = $url_arr['host'];
  22.     $path = $url_arr['path'] ."?".$url_arr['query'];
  23.     $port = isset($url_arr['port']) ?$url_arr['port'] : "80";
  24.     $fp = fsockopen($host, $port, $err_no, $err_str,30);
  25.     if (!$fp) return false;
  26.     $request_str = "GET ".$path." HTTP/1.1\r\n";
  27.     $request_str .= "Host:".$host."\r\n";
  28.     $request_str .= "Connection:Close\r\n\r\n";
  29.     fwrite($fp,$request_str);
  30.     $first_header = fread($fp, 128);
  31.     fclose($fp);
  32.     if (trim($first_header) == "") return false;
  33.     if (!preg_match("/200/", $first_header) || preg_match("/Location:/", $first_header)) return false;
  34.     return true;
  35.   }
  36. $url = "http://image.verylifes.com/webimages/78620c944c3a_9B13/201008230321.jpg";
  37. if (remote_file_exists($url)) {
  38.     $headInf = get_headers($url,1);
  39.     dump($headInf);
  40. }
  41. //输出结果
  42. //Array
  43. //(
  44. //    [0] => HTTP/1.1 200 OK
  45. //    [Date] => Thu, 26 Aug 2010 02:51:07 GMT
  46. //    [Server] => Apache
  47. //    [Last-Modified] => Mon, 23 Aug 2010 03:01:54 GMT
  48. //    [ETag] => "20a9e05-e220-4c71e4a2"
  49. //    [Accept-Ranges] => bytes
  50. //    [Content-Length] => 57888
  51. //    [Connection] => close
  52. //    [Content-Type] => image/jpeg
  53. //)
2010年8月26日 | 归档于 关注网络
本文目前尚无任何评论.
emoticons

发表评论