Page 1 of 1

skip blank space (regex)

Posted: Thu Oct 25, 2007 7:28 am
by aye
Hi,
I'm working on a function that reads a configuration file. First it reads the whole file and splits up every line into an array, then it reads every line for a configuration option and its respective value.
I.e. the configuration file could look like this:

Code: Select all

# Comment

cfg_option = 'value'
each line is then read using the following php code in a foreach loop

Code: Select all

// Don't read commented or empty lines
if( substr( $line, 0, substr($string, 0, 1 - strlen( $string ) ) ) == '#' || empty( $line ) ) continue;
			
preg_match( '/(.*) = [\'|"](.*)[\'|"]/', $line, $values );
This works fine, although I would like it to be able to read config files that doesn't follow this strict 'option*space*=*space*value' pattern. eg it should be able to read a line, that looks something like this as well

Code: Select all

cfg_option=        'value'
where it doesn't care whether there are four blanks or no blanks at all.

Any help would be great :)
thanks.

Posted: Thu Oct 25, 2007 7:42 am
by VladSun

Code: Select all

\s*
is for zero or more spaces

Posted: Thu Oct 25, 2007 8:08 am
by aye
thanks, works great now! :)