Forum Moderators: coopster
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
$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 ;)
We also have a nice thread on Regular Expressions [webmasterworld.com] where this is explained.
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
>>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 :(