├── .gitattributes ├── .gitignore ├── INI.class.php ├── README.md ├── config.ini └── example.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /INI.class.php: -------------------------------------------------------------------------------- 1 | 6 | * Last modified: 29 06 2013 7 | * Copyright (C): 2011 IT-radionica.com, All Rights Reserved 8 | * 9 | *** GNU General Public License (Version 2, June 1991) 10 | * 11 | * This program is free software; you can redistribute 12 | * it and/or modify it under the terms of the GNU 13 | * General Public License as published by the Free 14 | * Software Foundation; either version 2 of the License, 15 | * or (at your option) any later version. 16 | * 17 | * This program is distributed in the hope that it will 18 | * be useful, but WITHOUT ANY WARRANTY; without even the 19 | * implied warranty of MERCHANTABILITY or FITNESS FOR A 20 | * PARTICULAR PURPOSE. See the GNU General Public License 21 | * for more details. 22 | * 23 | *** Description 24 | * 25 | * Simple PHP Class to manage INI files (read and write). 26 | * 27 | ******************************************************************* 28 | * 29 | ** Examples 30 | * 31 | 32 | // Parse config.ini 33 | $ini = new INI('config.ini'); 34 | 35 | echo '
'; 36 | echo 'Content of: config.ini' . PHP_EOL; 37 | print_r($ini->data); 38 | 39 | // Udate settings 40 | $ini->data['first_section']['animal'] = 'COW'; 41 | 42 | // Save settings to file 43 | $ini->write(); 44 | 45 | // Update settings 46 | $ini->data['first_section']['animal'] = 'HORSE'; 47 | 48 | // Add new setting to section third_section 49 | $ini->data['third_section']['phpversion'][] = 5.4; 50 | 51 | // Add new section third_section and new item something 52 | $ini->data['fourth_section']['something'] = 'some data'; 53 | 54 | // Save settings to new file 55 | $ini->write('config-2.ini'); 56 | 57 | // INI obj is now using ini 2 file 58 | echo '
Content of: config-2.ini' . PHP_EOL; 59 | print_r($ini->data); 60 | 61 | // Parse config.ini 62 | $ini->read('config.ini'); 63 | 64 | // Remove item from second_section 65 | unset($ini->data['second_section']['URL']); 66 | 67 | // Remove third_section from second ini file and save to third file 68 | unset($ini->data['third_section']); 69 | 70 | // Save settings to new file 71 | $ini->write('config-3.ini'); 72 | 73 | // INI obj is now using ini 3 file 74 | echo '
Content of: config-3.ini' . PHP_EOL; 75 | print_r($ini->data); 76 | 77 | ******************************************************************/ 78 | 79 | class INI 80 | { 81 | /** INI file path 82 | * @var String 83 | */ 84 | var $file = NULL; 85 | 86 | /** INI data 87 | * @var Array 88 | */ 89 | var $data = array(); 90 | 91 | /** Process sections 92 | * @var Boolean 93 | */ 94 | var $sections = TRUE; 95 | 96 | /** Parse INI file 97 | * @param String $file - INI file path 98 | * @param Boolean $sections - Process sections 99 | */ 100 | function INI() { 101 | if (func_num_args()) { 102 | $args = func_get_args(); 103 | call_user_func_array(array($this, 'read'), $args); 104 | } 105 | } 106 | 107 | /** Parse INI file 108 | * @param String $file - INI file path 109 | * @param Boolean $sections - Process sections 110 | */ 111 | function read($file = NULL, $sections = TRUE) { 112 | $this->file = ($file) ? $file : $this->file; 113 | $this->sections = $sections; 114 | $this->data = parse_ini_file(realpath($this->file), $this->sections); 115 | return $this->data; 116 | } 117 | 118 | /** Write INI file 119 | * @param String $file - INI file path 120 | * @param Array $data - Data (Associative Array) 121 | * @param Boolean $sections - Process sections 122 | */ 123 | function write($file = NULL, $data = array(), $sections = TRUE) { 124 | $this->data = (!empty($data)) ? $data : $this->data; 125 | $this->file = ($file) ? $file : $this->file; 126 | $this->sections = $sections; 127 | $content = NULL; 128 | 129 | if ($this->sections) { 130 | foreach ($this->data as $section => $data) { 131 | $content .= '[' . $section . ']' . PHP_EOL; 132 | foreach ($data as $key => $val) { 133 | if (is_array($val)) { 134 | foreach ($val as $v) { 135 | $content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL; 136 | } 137 | } elseif (empty($val)) { 138 | $content .= $key . ' = ' . PHP_EOL; 139 | } else { 140 | $content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL; 141 | } 142 | } 143 | $content .= PHP_EOL; 144 | } 145 | } else { 146 | foreach ($this->data as $key => $val) { 147 | if (is_array($val)) { 148 | foreach ($val as $v) { 149 | $content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL; 150 | } 151 | } elseif (empty($val)) { 152 | $content .= $key . ' = ' . PHP_EOL; 153 | } else { 154 | $content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL; 155 | } 156 | } 157 | } 158 | 159 | return (($handle = fopen($this->file, 'w')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE; 160 | } 161 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP_INI_Read_Write 2 | ================== 3 | 4 | This class can read and write configuration values in INI files. 5 | 6 | It can parse a given INI file and extract the contained configuration values into class array variables. 7 | 8 | The class can also do the opposite, i.e., write the configuration values in the array variable back to a INI file. 9 | 10 | 11 | Examples: 12 | 13 | 14 | `````php 15 | // Parse config.ini 16 | $ini = new INI('config.ini'); 17 | 18 | // Content of: config.ini 19 | print_r($ini->data); 20 | 21 | // Udate settings 22 | $ini->data['first_section']['animal'] = 'COW'; 23 | 24 | // Save settings to file 25 | $ini->write(); 26 | 27 | // Update settings 28 | $ini->data['first_section']['animal'] = 'HORSE'; 29 | 30 | // Add new setting to section third_section 31 | $ini->data['third_section']['phpversion'][] = 5.4; 32 | 33 | // Add new section third_section and new item something 34 | $ini->data['fourth_section']['something'] = 'some data'; 35 | 36 | // Save settings to new file 37 | $ini->write('config-2.ini'); 38 | 39 | // INI obj is now using ini 2 file 40 | // Content of: config-2.ini 41 | print_r($ini->data); 42 | 43 | // Parse config.ini 44 | $ini->read('config.ini'); 45 | 46 | // Remove item from second_section 47 | unset($ini->data['second_section']['URL']); 48 | 49 | // Remove third_section from second ini file and save to third file 50 | unset($ini->data['third_section']); 51 | 52 | // Save settings to new file 53 | $ini->write('config-3.ini'); 54 | 55 | // INI obj is now using ini 3 file 56 | // Content of: config-3.ini 57 | print_r($ini->data); 58 | ````` 59 | -------------------------------------------------------------------------------- /config.ini: -------------------------------------------------------------------------------- 1 | [first_section] 2 | one = 1 3 | five = 5 4 | animal = "COW" 5 | 6 | [second_section] 7 | path = "/usr/local/bin" 8 | URL = "http://www.example.com/~username" 9 | 10 | [third_section] 11 | phpversion[] = 5.0 12 | phpversion[] = 5.1 13 | phpversion[] = 5.2 14 | phpversion[] = 5.3 -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | '; 13 | echo 'Content of: config.ini' . PHP_EOL; 14 | print_r($ini->data); 15 | 16 | // Udate settings 17 | $ini->data['first_section']['animal'] = 'COW'; 18 | 19 | // Save settings to file 20 | $ini->write(); 21 | 22 | // Update settings 23 | $ini->data['first_section']['animal'] = 'HORSE'; 24 | 25 | // Add new setting to section third_section 26 | $ini->data['third_section']['phpversion'][] = 5.4; 27 | 28 | // Add new section third_section and new item something 29 | $ini->data['fourth_section']['something'] = 'some data'; 30 | 31 | // Save settings to new file 32 | $ini->write('config-2.ini'); 33 | 34 | // INI obj is now using ini 2 file 35 | echo '
Content of: config-2.ini' . PHP_EOL; 36 | print_r($ini->data); 37 | 38 | // Parse config.ini 39 | $ini->read('config.ini'); 40 | 41 | // Remove item from second_section 42 | unset($ini->data['second_section']['URL']); 43 | 44 | // Remove third_section from second ini file and save to third file 45 | unset($ini->data['third_section']); 46 | 47 | // Save settings to new file 48 | $ini->write('config-3.ini'); 49 | 50 | // INI obj is now using ini 3 file 51 | echo '
Content of: config-3.ini' . PHP_EOL; 52 | print_r($ini->data); --------------------------------------------------------------------------------