skip blank space (regex)

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
aye
Forum Commoner
Posts: 25
Joined: Wed Apr 11, 2007 9:02 am

skip blank space (regex)

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

\s*
is for zero or more spaces
There are 10 types of people in this world, those who understand binary and those who don't
aye
Forum Commoner
Posts: 25
Joined: Wed Apr 11, 2007 9:02 am

Post by aye »

thanks, works great now! :)
Post Reply