Page 1 of 1

cant escape curly brackets

Posted: Wed Nov 02, 2011 4:25 pm
by superdaveozzborn
i have been searching for some time now for the answer to my problem, i have a css file that i want to alter using php and a form.

i have an issue with the curly brackets.

the source:

Code: Select all

body { background-color: #FCDC8E; }


the code:

Code: Select all

preg_match("/body \{ background-color: #(.*?); \}/i",$theData, $obody);
if i shorten the pattern down to
background-color: #(.*?);
i can get the color fine
but it is necessary for me to use the curly brackets in order to get the correct body statement from the file.

is it possible to escape curly brackets in order for this to work.

Re: cant escape curly brackets

Posted: Wed Nov 02, 2011 6:13 pm
by superdaveozzborn
forgot to mention i am running a lamp server with php5 and apache2
i have tried several different regular expression testers on line and this seams to work,
http://lumadis.be/regex/test_regex.php?id=923

Code: Select all

preg_match("/body \{ background-color: #(.*?); \}/i",$theData, $obody);
echo $obody[1];
however when i try it on my server i get nothing.
is this even possible?
any help would be grate.

Thank You.

Re: cant escape curly brackets

Posted: Thu Nov 03, 2011 1:30 pm
by superdaveozzborn
any one?

is this possible?

Re: cant escape curly brackets

Posted: Thu Nov 03, 2011 1:36 pm
by Weirdan
I believe you need two backslashes (one for PHP string escaping and another one for regex escaping):

Code: Select all

preg_match("/body \\{ background-color: #(.*?); \\}/i",$theData, $obody);

Re: cant escape curly brackets

Posted: Thu Nov 03, 2011 1:44 pm
by AbraCadaver
Yes, and this might work as well:
[text]'/body \{ background-color: #(.*?); \}/i'[/text]

Re: cant escape curly brackets

Posted: Sat Nov 05, 2011 4:20 pm
by superdaveozzborn
this is what i thought, however the above does not work for some reason. the only way i can get it to work is to remove the curly brackets then i can retrieve the color code, which poses a new problem as i have several "background-color:" in the css file and need to match the entire "body { }" section, i am unable to escape the curly brackets in this manner. the "\" is not working on the curly brackets but it works on every thing else as intended. i am using php5 here on a LAMP server on an Ubuntu machine. has something changed in php5.

Thank you for your time please any idea what this issue is. i will provide more info as needed.

Re: cant escape curly brackets

Posted: Sat Nov 05, 2011 4:51 pm
by Weirdan

Code: Select all

<?php
    $source = "body { background-color: #FCDC8E; }";
    
    preg_match("/body \\{ background-color: #(.*?); \\}/i", $source, $matches);
    var_dump($matches);
works for me

Re: cant escape curly brackets

Posted: Sat Nov 05, 2011 5:33 pm
by AbraCadaver

Code: Select all

$source = "body { background-color: #FCDC8E; }";
preg_match('/body \{ background-color: #(.*?); \}/i', $source, $matches);
var_dump($matches);
Works for me.
[text]array(2) {
[0]=>
string(35) "body { background-color: #FCDC8E; }"
[1]=>
string(6) "FCDC8E"
}[/text]

PHP 5.3.5

Re: cant escape curly brackets

Posted: Wed Nov 09, 2011 2:36 pm
by superdaveozzborn
ok now i see the error of my ways. there were a few crlf and white spaces in the file that were not shown in the string. when i displayed the string there were no crlf characters so i overlooked them after closely reviewing the file it is layed out like this

Code: Select all

body {
	background-color: #FCDC8E;
}
not like the string showed which was this.

Code: Select all

body { background-color: #FCDC8E; }
what i had to do was user \s a few times.

Code: Select all

/body \{\s*background-color: #(.*?);\s\}/i
Thank you to all who helped it was appreciated very much.

Re: cant escape curly brackets

Posted: Wed Nov 09, 2011 3:43 pm
by AbraCadaver
I would try this, maybe more flexible:
[text]/body[\s]*\{[\s]*background-color:[\s]*#(.*?);[\s]*\}/is[/text]