473,240 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

php's preg_match_all() and css classes...

Hello,

I'm trying to parse out the properties of a class definition from a css
file and am running into issues trying to write the reg. expression:

h1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold;
color: #003399;
}
....

I need the results in:
array[i][0] = "h1"
array[i][1] = "
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold;
color: #003399;"

Any idea? I tried looking online for an example and can't find anything
that works with php's preg_match_all() function...

Thank you in advance!

-Kevin

Sep 12 '05 #1
2 3876
kevinC said the following on 12/09/2005 21:38:
Hello,

I'm trying to parse out the properties of a class definition from a css
file and am running into issues trying to write the reg. expression:

h1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold;
color: #003399;
}
...

I need the results in:
array[i][0] = "h1"
array[i][1] = "
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold;
color: #003399;"

Any idea? I tried looking online for an example and can't find anything
that works with php's preg_match_all() function...


preg_match_all('/\s*(.*)\s*\{\s*(.*)\s*\}/sU', $str, $array,
PREG_SET_ORDER);
P.S. Please cross-post rather than multi-post.

--
Oli
Sep 12 '05 #2
In article <11**********************@g49g2000cwa.googlegroups .com>,
"kevinC" <kc*******@gmail.com> wrote:
Hello,

I'm trying to parse out the properties of a class definition from a css
file and am running into issues trying to write the reg. expression:

h1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold;
color: #003399;
}
...

I need the results in:
array[i][0] = "h1"
array[i][1] = "
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold;
color: #003399;"

Any idea? I tried looking online for an example and can't find anything
that works with php's preg_match_all() function...


This is how I solved it:

<?
# read stylesheets from a file
$filen=join("", file($stylesheetfile));

# remove everything between /* and */
$filen = preg_replace("!/\*.*?\*/!ms", "", $filen);

# remove whitespaces after semicolons
$filen = preg_replace("/;\s+/m", "; ", $filen);

# remove whitespaces after {
$filen = preg_replace("/{\s+/m", "{ ", $filen);

# remove whitespace before {
$filen = preg_replace("/\s+{/m", " {", $filen);

# replace several newlines with one
$filen = preg_replace("/\n{2,}/m", "\n", $filen);

# Leading whitespace
$filen = preg_replace("/^\s*/m", "", $filen);

# Multiple whitespaces to one
$filen = preg_replace("/ +/m", " ", $filen);

# Split every row and put in array as:
# filestylearray[selector][attribute]="value";

foreach(split("\n", $filen) as $line){
preg_match("/^(.*){\s*(.*)\s*;\s*}\s*$/", $line, $m);
foreach(split(";",$m[2]) as $attribline){
$a=split(":", trim($attribline));
$stylearray[strtolower(trim($m[1]))][trim($a[0])]=trim("$a[1]");
}
}

?>

It doesn't sort it like you want it to, but it can be tweaked.

Your example would end up in:

Array
(
[h1] => Array
(
[font-family] => Verdana, Arial, Helvetica, sans-serif
[font-size] => 16px
[font-weight] => bold
[color] => #003399
)

)


--
Sandman[.net]
Sep 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out...
6
by: Zeeshan | last post by:
Hi Everyone, How can I get all the values which are between Name tags as follows: <emp> <name>N1</name> <age>1</age> <name>N2</name> <age>2</age>
10
by: greatprovider | last post by:
i'm starting with a string such as "Na**3C**6H**5O**7*2H**20" im attempting to match all **\d+ ...once i can match all the double asterix \d i intend to wrap the \d in "<sub>" tags for display...
2
by: ameshkin | last post by:
This script I wrote works with tables, td's and div's, but not with style tags. Can anyone figure out the regular expression for finding <styletags. The trick is that sometimes its not just...
5
by: seddy | last post by:
Hello ! I`m kinda new to it so I found this `job` very hard therefor I ask for Your help. So, the thing is... I have this XML file ( http://www.izishop.net/export.php ) which I need to open...
2
by: David | last post by:
Hi, Could PHP be used to take a txt file (or set of txt files) and add a string of characters every X number of words or characters? Say a txt file with 50,000 characters/5,000 words how would...
8
by: Damo | last post by:
Hi, I'm new to this group and regular expressions. I want to extract text from a newspaper website using regular expressions and php I'm using this regular expression at the moment $regexp =...
10
by: swethak | last post by:
hi, i write the below code to capture the images from the website when i submit the url.In the same way i want to capture the Text information from the website.plz tell that whats the code for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.