473,386 Members | 2,078 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,386 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 3894
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.