473,804 Members | 3,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3922
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************ **********@g49g 2000cwa.googleg roups.com>,
"kevinC" <kc*******@gmai l.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($styleshee tfile));

# 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($attriblin e));
$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
4698
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 = 3456789 preg_match ("|product=(\d{5,10})&amp;|i", $html, $out);
6
2209
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
2045
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 purposes. i have been trying to write the correct pattern for 2 weeks now without any success...i can get preg_replace() to work, with several simple patterns but when i use preg_match_all, i either get unintended results or incomplete matches.
2
12262
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 <style Its <style type="text/css"> Basically, i want to take the information in between the style content from any url <?php
5
17282
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 with php and read it, and insert some fields those fields on my MySQL database. Well... i found some usefull informations which might help me here: (http://www.thescripts.com/forum/thread2005.html) Take that data
2
2191
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 you go about adding a string of characters every 5,000 characters or 500 words. To improve on this I'd want to if using characters as the guide to use
8
1562
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 = "%<table cellspacing=\"0\" cellpadding=\"0\"(.+) <img height=\"1\" alt=\"Today's News\" />%s"; Each news story is in between those tags , So if I extract those chunks of html using
10
2700
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 that.plz help me. <?php $content= file_get_contents($url);
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.