Xini file parser v1.0

<?
  
  # Xini parser v1.0 by MC Solutions 2007, all righs reserved
  
  /*
    Samlpe file:
    
    menu.xini:
    
    !Structure=Text,Url

    [Menu]
    Item="Om firmaet","about.php"
    Item="Kontakt firmaet","contact.php"

    [Menu2]
    Item="Bestil","order.php"
    Item="Log ind","login.php","nisse","fisk"


    [forside]
    Titel="Bla bla bla"
    BestilLink="Bla bla bla"
    
    
    EOF
    
    !Structure describes the name of the coma separated variables, used for generation an associative array.
    
    Lines starting with ; or # are ignores ie. comments
    
    The result of parsing the file ahead will be:
    
    Array
    (
        [Menu] => Array
    	    (
    	        [0] => Array
                (
                    [Text] => Om firmaet
                    [Url] => about.php
                )

    	        [1] => Array
                (
                    [Text] => Kontakt firmaet
                    [Url] => contact.php
                )

        	)

    		[Menu2] => Array
        	(
            	[0] => Array
                (
                    [Text] => Bestil
                    [Url] => order.php
                )

            	[1] => Array
                (
                    [Text] => Log ind
                    [Url] => login.php
                    [0] => nisse
                    [1] => fisk
                )

        	)

    		[forside] => Array
        	(
        	    [Titel] => Bla bla bla
          	    [BestilLink] => Bla bla bla
        	)

  )

  
  */
  

  
  class XiniParser
  {
    public function parse($file)
    {
      $data=preg_split("/\n/",file_get_contents($file));
      foreach ($data as $number=>$line)
      {
        $tokkens=preg_split("/=|\ /",$line,-1,PREG_SPLIT_NO_EMPTY);	
        if (($tokkens[0]=="") or ($tokkens[0][0]=="#") or ($tokkens[0][0]==";"))
        {
          // Tom Linie eller kommentar
        }
        else if ($tokkens[0]=="!Structure")
        {
          //print "NEW STRUC:$line\n";
          $struct=preg_split('/,/',$tokkens[1],-1,PREG_SPLIT_NO_EMPTY);
      
          
          $keys=$struct;
      
        } else if ($tokkens[0][0]=='[')
        {
          $block=preg_split('/\[|\]/',$line);
          $block=$block[1];		
        } else {
      
          $tokkens=preg_split("/=/",$line);
      
          $values=preg_split('/","/',$tokkens[1]);
      
          if (count($values)==1)
          {
            $value=substr($values[0],1,-1);	// Remove tailing and heading "
          } else {
            $values[0]=substr($values[0],1);
            $values[count($values)-1]=substr($values[count($values)-1],0,-1);
            unset($value);
            foreach ($values as $k=>$v)
            {
              if ($keys[$k]!="")
              {
                $value[$keys[$k]]=$v;
              } else {
                $value[]=$v;
              }
            }
        
          }	
            
          if ($tokkens[0]=="Item")
          {
            $ret[$block][]=$value;					
          } else {
            if (strpos($line,"="))
            {
              $ret[$block][$tokkens[0]]=$value;
            
              // Value Assignments	
            } else 
            {
              print "syntax error at line $number:`<i>$line</i>`<br>";
            }	
          }
        }		
      }
      return $ret;
    }
  }
  
  $xini=new XiniParser();
  
  print_r($xini->parse('menu.xini'));
  
?>

 

Dette indlæg blev udgivet i Knowledge Base, Old Base, Programmering. Bogmærk permalinket.

Skriv et svar