Cheetah
var_export.php
Go to the documentation of this file.
1 <?php
2 
8 // +----------------------------------------------------------------------+
9 // | PHP Version 4 |
10 // +----------------------------------------------------------------------+
11 // | Copyright (c) 1997-2004 The PHP Group |
12 // +----------------------------------------------------------------------+
13 // | This source file is subject to version 3.0 of the PHP license, |
14 // | that is bundled with this package in the file LICENSE, and is |
15 // | available at through the world-wide-web at |
16 // | http://www.php.net/license/3_0.txt. |
17 // | If you did not receive a copy of the PHP license and are unable to |
18 // | obtain it through the world-wide-web, please send a note to |
19 // | license@php.net so we can mail you a copy immediately. |
20 // +----------------------------------------------------------------------+
21 // | Authors: Aidan Lister <aidan@php.net> |
22 // +----------------------------------------------------------------------+
23 //
24 // $Id: var_export.php,v 1.2 2005/11/21 10:57:23 ggiunta Exp $
25 
26 
38 if (!function_exists('var_export')) {
39  function var_export($array, $return = false, $lvl=0)
40  {
41  // Common output variables
42  $indent = ' ';
43  $doublearrow = ' => ';
44  $lineend = ",\n";
45  $stringdelim = '\'';
46 
47  // Check the export isn't a simple string / int
48  if (is_string($array)) {
49  $out = $stringdelim . str_replace('\'', '\\\'', str_replace('\\', '\\\\', $array)) . $stringdelim;
50  } elseif (is_int($array) || is_float($array)) {
51  $out = (string)$array;
52  } elseif (is_bool($array)) {
53  $out = $array ? 'true' : 'false';
54  } elseif (is_null($array)) {
55  $out = 'NULL';
56  } elseif (is_resource($array)) {
57  $out = 'resource';
58  } else {
59  // Begin the array export
60  // Start the string
61  $out = "array (\n";
62 
63  // Loop through each value in array
64  foreach ($array as $key => $value) {
65  // If the key is a string, delimit it
66  if (is_string($key)) {
67  $key = str_replace('\'', '\\\'', str_replace('\\', '\\\\', $key));
68  $key = $stringdelim . $key . $stringdelim;
69  }
70 
71  $val = var_export($value, true, $lvl+1);
72  // Delimit value
73  /*if (is_array($value)) {
74  // We have an array, so do some recursion
75  // Do some basic recursion while increasing the indent
76  $recur_array = explode($newline, var_export($value, true));
77  $temp_array = array();
78  foreach ($recur_array as $recur_line) {
79  $temp_array[] = $indent . $recur_line;
80  }
81  $recur_array = implode($newline, $temp_array);
82  $value = $newline . $recur_array;
83  } elseif (is_null($value)) {
84  $value = 'NULL';
85  } else {
86  $value = str_replace($find, $replace, $value);
87  $value = $stringdelim . $value . $stringdelim;
88  }*/
89 
90  // Piece together the line
91  for ($i = 0; $i < $lvl; $i++)
92  $out .= $indent;
93  $out .= $key . $doublearrow . $val . $lineend;
94  }
95 
96  // End our string
97  for ($i = 0; $i < $lvl; $i++)
98  $out .= $indent;
99  $out .= ")";
100  }
101 
102  // Decide method of output
103  if ($return === true) {
104  return $out;
105  } else {
106  echo $out;
107  return;
108  }
109  }
110 }
111 ?>
php
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10