cant escape curly brackets

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

Moderator: General Moderators

Post Reply
superdaveozzborn
Forum Newbie
Posts: 5
Joined: Wed Nov 02, 2011 4:14 pm

cant escape curly brackets

Post 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.
superdaveozzborn
Forum Newbie
Posts: 5
Joined: Wed Nov 02, 2011 4:14 pm

Re: cant escape curly brackets

Post 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.
superdaveozzborn
Forum Newbie
Posts: 5
Joined: Wed Nov 02, 2011 4:14 pm

Re: cant escape curly brackets

Post by superdaveozzborn »

any one?

is this possible?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: cant escape curly brackets

Post 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);
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: cant escape curly brackets

Post by AbraCadaver »

Yes, and this might work as well:
[text]'/body \{ background-color: #(.*?); \}/i'[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
superdaveozzborn
Forum Newbie
Posts: 5
Joined: Wed Nov 02, 2011 4:14 pm

Re: cant escape curly brackets

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: cant escape curly brackets

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: cant escape curly brackets

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
superdaveozzborn
Forum Newbie
Posts: 5
Joined: Wed Nov 02, 2011 4:14 pm

Re: cant escape curly brackets

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: cant escape curly brackets

Post by AbraCadaver »

I would try this, maybe more flexible:
[text]/body[\s]*\{[\s]*background-color:[\s]*#(.*?);[\s]*\}/is[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply