Forum Moderators: coopster

Message Too Old, No Replies

Regex

parsing config file with $site = "http://"

         

mcibor

9:56 pm on Oct 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi All

I would like to find with preg_match, or similar function the text in quotes if the beginning of the line is correct.

I use
$config = array_map('rtrim', file('config.php')); //to get data

Then I would like to parse the text:
$sitename = "Name";
$sitemail = "mail@domain.com";
$siteadress = "http://www.domain.com/subdomain";

What I would like is to check if the beginning of the line is correct, and get Name and mail@domain.com into a variable.

I tried to accomplish it with below function, but cannot grasp the error

preg_match("/\\$sitename \"(^[A-Za-z0-9 -]*$)\";/" , $config[1] , $array_site);

However I get an error:
Warning: Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 1 in

But when I do it with
sitename \"(^[A-Za-z0-9 -]*$)\

I get no error and no results.

How to correct it? I'm not happy to use strpos and substr

Regards
Michal Cibor

eelixduppy

3:13 am on Oct 27, 2006 (gmt 0)



Try a pattern like this:

$pattern = "/\$sitename = \"(\w+)\"/";

Unfortunately my development computer is down, otherwise I'd check this solution, but it seems like it should work.

Best of luck!

P.S. regex isn't my specialty ;)

Psychopsia

4:37 am on Oct 27, 2006 (gmt 0)

10+ Year Member



hey eelixduppy, can you explain me please what is \w, because I can't find it in pattern modifiers [php.net].

eelixduppy

2:44 pm on Oct 27, 2006 (gmt 0)



It isn't a pattern modifier. It represents an alphanumeric character, including the underscore. Refer to Pattern Syntax [php.net] for more information.

We also have a nice thread on Regular Expressions [webmasterworld.com] where this is explained.

mcibor

8:48 pm on Oct 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks eelixduppy,

but I had to modify your solution. I got rid of finding the $ (didn't work either with \$ nor \\$ - no idea why

And \w doesn't work for me - I need to allow for such combinations as
D0m-ai n

So my code looks now:
$pattern = "/sitename = \"([A-Za-z0-9 -]+)\"/";

PS. My previous pattern didn't work because ^[A checks for pattern match at the beginning of the string, not the part after = ", the same with ending $

Moreover your using + instead of * is quite a good idea.

Thank you
Michal

eelixduppy

5:53 am on Oct 28, 2006 (gmt 0)



Glad you derived a fitting solution :)

>>didn't work either with \$ nor \\$ - no idea why

I think that surrounding the pattern with single quotes instead of double will get rid of this error the way we had it(\$). I would check this but my server is down :(

mcibor

4:05 pm on Oct 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm dumb :) certainly it won't work with "/\$sitename because php parses variables within double quotes. And yes, single ones will do fine.

Thanks for pointing that out - simple errors are hardest to spot.

Regards
Michal