Created
September 18, 2010 19:07
-
-
Save samkeen/585948 to your computer and use it in GitHub Desktop.
PHP Array Util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* array Util class | |
*/ | |
class Arr { | |
/** | |
* $array Can be either an Array or the string output of | |
* print_r($array, true); | |
*/ | |
public static function print_php($array) { | |
$array = is_array($array)?print_r($array,true):$array; | |
$array = preg_replace( | |
array( | |
'/Array\n/', | |
'/\[(.*)\] =>/', | |
'/\)\n/' | |
), | |
array( | |
"array\n", | |
'"${1}" =>', | |
"),\n" | |
), | |
$array); | |
// need regex skill++ , for now this will do | |
$array = preg_replace_callback( | |
'/=> (.*?)\n/', | |
// only replace if it is not the word 'array' or just digits | |
create_function( | |
'$matches', | |
'$match = isset($matches[1])?$matches[1]:null; | |
$qt = ctype_digit($match)||$match=="array"?"":"\""; | |
$c = $match=="array"?"":","; | |
return "=> {$qt}{$match}{$qt}{$c}\n";' | |
), | |
$array); | |
return $array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment