Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function loadPattern($file,$flatten = false)
- {
- if(is_file($file))
- {
- $jsonString = file_get_contents($file);
- }
- else
- {
- return null;
- }
- // Remove single-line comments (// ...)
- $jsonString = preg_replace('/\/\/[^\r\n\"]*$/', '', $jsonString);
- // Remove multi-line comments (/* ... */)
- $jsonString = preg_replace('/\/\*[\s\S]*?\*\//', '', $jsonString);
- // Trim whitespace
- $jsonString = trim($jsonString);
- json_decode($jsonString,true);
- if ( json_last_error ( ) !== JSON_ERROR_NONE )
- {
- return null;
- }
- $temp = json_decode($jsonString,true);
- if ($flatten === true && is_array($temp)) {
- $temp = $this->flattenArray($temp);
- }
- return $temp;
- }
- // Helper function to flatten a multi-dimensional array
- public function flattenArray(array $array)
- {
- $result = [];
- array_walk_recursive($array, function ($value) use (&$result) {
- $result[] = $value;
- });
- return implode("", $result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement