Advertisement
jargon

Linedancer (LD4.3) :: "LD4 Pattern Loader.php"

Jan 25th, 2025
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | Gaming | 0 0
  1.     public function loadPattern($file,$flatten = false)
  2.     {
  3.         if(is_file($file))
  4.         {
  5.             $jsonString = file_get_contents($file);
  6.         }
  7.         else
  8.         {
  9.             return null;
  10.         }
  11.        
  12.         // Remove single-line comments (// ...)
  13.         $jsonString = preg_replace('/\/\/[^\r\n\"]*$/', '', $jsonString);
  14.        
  15.         // Remove multi-line comments (/* ... */)
  16.         $jsonString = preg_replace('/\/\*[\s\S]*?\*\//', '', $jsonString);
  17.        
  18.         // Trim whitespace
  19.         $jsonString = trim($jsonString);
  20.        
  21.         json_decode($jsonString,true);
  22.        
  23.         if ( json_last_error ( ) !== JSON_ERROR_NONE )
  24.         {
  25.             return null;
  26.         }
  27.  
  28.         $temp = json_decode($jsonString,true);
  29.        
  30.         if ($flatten === true && is_array($temp)) {
  31.             $temp = $this->flattenArray($temp);
  32.         }
  33.        
  34.         return $temp;      
  35.     }
  36.    
  37.     // Helper function to flatten a multi-dimensional array
  38.     public function flattenArray(array $array)
  39.     {
  40.         $result = [];
  41.    
  42.         array_walk_recursive($array, function ($value) use (&$result) {
  43.             $result[] = $value;
  44.         });
  45.    
  46.         return implode("", $result);
  47.     }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement