Rev 130 Rev 139
Line 1... Line 1...
1 <?php 1 <?php
2 // +----------------------------------------------------------------------+ 2 // +----------------------------------------------------------------------+
3 // | PHP Version 4 | 3 // | PHP Version 4 |
4 // +----------------------------------------------------------------------+ 4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997-2004 The PHP Group | 5 // | Copyright (c) 1997-2004 The PHP Group |
6 // +----------------------------------------------------------------------+ 6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 3.0 of the PHP license, | 7 // | This source file is subject to version 3.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is | 8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at | 9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/3_0.txt. | 10 // | http://www.php.net/license/3_0.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to | 11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to | 12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. | 13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+ 14 // +----------------------------------------------------------------------+
15 // | Authors: Aidan Lister <aidan@php.net> | 15 // | Authors: Aidan Lister <aidan@php.net> |
16 // +----------------------------------------------------------------------+ 16 // +----------------------------------------------------------------------+
17 // 17 //
18 // $Id: var_export.php,v 1.15 2005/12/05 14:24:27 aidan Exp $ 18 // $Id: var_export.php,v 1.15 2005/12/05 14:24:27 aidan Exp $
19   19  
20   20  
21 /** 21 /**
22 * Replace var_export() 22 * Replace var_export()
23 * 23 *
24 * @category PHP 24 * @category PHP
25 * @package PHP_Compat 25 * @package PHP_Compat
26 * @link http://php.net/function.var_export 26 * @link http://php.net/function.var_export
27 * @author Aidan Lister <aidan@php.net> 27 * @author Aidan Lister <aidan@php.net>
28 * @version $Revision: 1.15 $ 28 * @version $Revision: 1.15 $
29 * @since PHP 4.2.0 29 * @since PHP 4.2.0
30 * @require PHP 4.0.0 (user_error) 30 * @require PHP 4.0.0 (user_error)
31 */ 31 */
32 if (!function_exists('var_export')) { 32 if (!function_exists('var_export')) {
33 function var_export($var, $return = false, $level = 0) 33 function var_export($var, $return = false, $level = 0)
34 { 34 {
35 // Init 35 // Init
36 $indent = ' '; 36 $indent = ' ';
37 $doublearrow = ' => '; 37 $doublearrow = ' => ';
38 $lineend = ",\n"; 38 $lineend = ",\n";
39 $stringdelim = '\''; 39 $stringdelim = '\'';
40 $newline = "\n"; 40 $newline = "\n";
41 $find = array(null, '\\', '\''); 41 $find = array(null, '\\', '\'');
42 $replace = array('NULL', '\\\\', '\\\''); 42 $replace = array('NULL', '\\\\', '\\\'');
43 $out = ''; 43 $out = '';
44 44
45 // Indent 45 // Indent
46 $level++; 46 $level++;
47 for ($i = 1, $previndent = ''; $i < $level; $i++) { 47 for ($i = 1, $previndent = ''; $i < $level; $i++) {
48 $previndent .= $indent; 48 $previndent .= $indent;
49 } 49 }
50   50  
51 // Handle each type 51 // Handle each type
52 switch (gettype($var)) { 52 switch (gettype($var)) {
53 // Array 53 // Array
54 case 'array': 54 case 'array':
55 $out = 'array (' . $newline; 55 $out = 'array (' . $newline;
56 foreach ($var as $key => $value) { 56 foreach ($var as $key => $value) {
57 // Key 57 // Key
58 if (is_string($key)) { 58 if (is_string($key)) {
59 // Make key safe 59 // Make key safe
60 for ($i = 0, $c = count($find); $i < $c; $i++) { 60 for ($i = 0, $c = count($find); $i < $c; $i++) {
61 $var = str_replace($find[$i], $replace[$i], $var); 61 $var = str_replace($find[$i], $replace[$i], $var);
62 } 62 }
63 $key = $stringdelim . $key . $stringdelim; 63 $key = $stringdelim . $key . $stringdelim;
64 } 64 }
65 65
66 // Value 66 // Value
67 if (is_array($value)) { 67 if (is_array($value)) {
68 $export = var_export($value, true, $level); 68 $export = var_export($value, true, $level);
69 $value = $newline . $previndent . $indent . $export; 69 $value = $newline . $previndent . $indent . $export;
70 } else { 70 } else {
71 $value = var_export($value, true, $level); 71 $value = var_export($value, true, $level);
72 } 72 }
73   73  
74 // Piece line together 74 // Piece line together
75 $out .= $previndent . $indent . $key . $doublearrow . $value . $lineend; 75 $out .= $previndent . $indent . $key . $doublearrow . $value . $lineend;
76 } 76 }
77   77  
78 // End string 78 // End string
79 $out .= $previndent . ')'; 79 $out .= $previndent . ')';
80 break; 80 break;
81   81  
82 // String 82 // String
83 case 'string': 83 case 'string':
84 // Make the string safe 84 // Make the string safe
85 for ($i = 0, $c = count($find); $i < $c; $i++) { 85 for ($i = 0, $c = count($find); $i < $c; $i++) {
86 $var = str_replace($find[$i], $replace[$i], $var); 86 $var = str_replace($find[$i], $replace[$i], $var);
87 } 87 }
88 $out = $stringdelim . $var . $stringdelim; 88 $out = $stringdelim . $var . $stringdelim;
89 break; 89 break;
90   90  
91 // Number 91 // Number
92 case 'integer': 92 case 'integer':
93 case 'double': 93 case 'double':
94 $out = (string) $var; 94 $out = (string) $var;
95 break; 95 break;
96 96
97 // Boolean 97 // Boolean
98 case 'boolean': 98 case 'boolean':
99 $out = $var ? 'true' : 'false'; 99 $out = $var ? 'true' : 'false';
100 break; 100 break;
101   101  
102 // NULLs 102 // NULLs
103 case 'NULL': 103 case 'NULL':
104 case 'resource': 104 case 'resource':
105 $out = 'NULL'; 105 $out = 'NULL';
106 break; 106 break;
107   107  
108 // Objects 108 // Objects
109 case 'object': 109 case 'object':
110 // Start the object export 110 // Start the object export
111 $out = $newline . $previndent . 'class ' . get_class($var) . ' {' . $newline; 111 $out = $newline . $previndent . 'class ' . get_class($var) . ' {' . $newline;
112   112  
113 // Export the object vars 113 // Export the object vars
114 foreach (get_object_vars($var) as $key => $val) { 114 foreach (get_object_vars($var) as $key => $val) {
115 $out .= $previndent . ' var $' . $key . ' = '; 115 $out .= $previndent . ' var $' . $key . ' = ';
116 if (is_array($val)) { 116 if (is_array($val)) {
117 $export = var_export($val, true, $level); 117 $export = var_export($val, true, $level);
118 $out .= $newline . $previndent . $indent . $export . ';' . $newline; 118 $out .= $newline . $previndent . $indent . $export . ';' . $newline;
119 } else { 119 } else {
120 $out .= var_export($val, true, $level) . ';' . $newline; 120 $out .= var_export($val, true, $level) . ';' . $newline;
121 } 121 }
122 } 122 }
123 $out .= $previndent . '}'; 123 $out .= $previndent . '}';
124 break; 124 break;
125 } 125 }
126   126  
127 // Method of output 127 // Method of output
128 if ($return === true) { 128 if ($return === true) {
129 return $out; 129 return $out;
130 } else { 130 } else {
131 echo $out; 131 echo $out;
132 } 132 }
133 } 133 }
134 } 134 }
135   135  
136 ?> 136 ?>
137 137