Version: 1.0

Type: Function

Category: Other

License: GNU General Public License

Description: I'm sure there's others. Includes a small function to strip whitespace.



<?
	/********************************************************************************\
		Here's example ini settings:

		Lines beginning with ; are obviously ignored.

		; returns $array['foo'] = "bar"
		foo=bar

		; returns $array['foo'] = "bar.com/?foo=bar"
		foo=bar.com/?foo=bar

		; returns $array['foo'][0] = "foo" and $array['foo'][1] = "bar"
		foo=foo,bar
	
	\********************************************************************************/

	// strip a string of whitespace
	function ws_replace( $string )
	{
		$match = array("/ /","/\t/","/\r/","/\n/");
		$replacement = "";
		$new_string = preg_replace( $match , $replacement , $string );
		return $new_string;
	}

	// gather settings from specified ini file
	function settings( $ini )
	{
		$fp = 0;		// the ini's file pointer
		$string = "";		// where to put the line
		$option = "";		// the name of the option
		$value = "";		// the value of the option
		$multi = "";		// if commas are used in value, it has multiple values
		$settings = array();	// this is where we'll dump all the ini's settings

		// attempt to open ini file
		if( $fp = @fopen( $ini , "r" ) )
		{
			// while we haven't reached the end of the file...
			while( !feof( $fp ) )
			{
				// get next line and strip it of whitespace
				$string = ws_replace( fgets( $fp , 1024 ) );

				// if the line begins with ; skip on to the next line
				if ( $string{0} == ";" )
				{
					continue;
				}

				// if the line is not a proper settings, ignore it
				elseif ( !eregi( "=" , $string ) )
				{
					continue;
				}

				// if we're here, than we make a setting
				else
				{
					// assign the option and value
					list( $option , $value ) = split( "=" , $string , 2 );

					// if $value contains commas, assign the value to an array
					if ( eregi( "," , $value ) )
					{
						$multi = explode( "," , $value );

						// $i<254 sets the array to a limit of 255 values
						for ($i=0;$i<254;$i++)
						{
							// if $i exists and isn't empty assign the key a value
							if ( array_key_exists( $i , $multi ) && !empty( $multi[$i] ) )
							{
								$settings[$option][$i] = $multi[$i];
							}

							// otherwise, break the for loop
							else
							{
								break;
							}
						}
					}

					// else assign normally
					else
					{
						$settings[$option] = $value;
					}
				}
			}

			fclose( $fp );
			return $settings;
		}

		// if we can't open the ini file, return false
		else
		{
			return false;
		}
	}
?>