473,387 Members | 1,520 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,387 software developers and data experts.

Extract Content Between Style Tags with Preg and PHP

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

$url = "http://www.anyurlwithstyletags.com";
$html = file_get_contents($url);

//preg_match_all("/<style.*style>/", $html, $matches);
preg_match_all("/<div.*div>/", $html, $matches);
//preg_match_all("/[\s]*[<style>][\s]*[\w]*[\s]*[<\\style>][\s]*/",
$string, $matches);
//preg_match_all("/(\<[
\\n\\r\\t]{0,}style[^>]*\>|\<[^>]*[\\n\\r\\t]{1,}style[^>]*\>){1}([^<]*<([^(\/>)]*(\/[^(t>)]){0,1}(\/t[^(r>)]){0,1})*>)*(\<[
\\n\\r\\t]{0,}\/tr[^>]*\>|\<[^>]*[\\n\\r\\t]{1,}\/style[^>]*\>){1}/i",
$html, $Matches);

foreach($matches[0] as $div) {
echo $div;

}

?>

Sep 8 '06 #1
2 12154

ameshkin wrote:
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

$url = "http://www.anyurlwithstyletags.com";
$html = file_get_contents($url);

//preg_match_all("/<style.*style>/", $html, $matches);
preg_match_all("/<div.*div>/", $html, $matches);
//preg_match_all("/[\s]*[<style>][\s]*[\w]*[\s]*[<\\style>][\s]*/",
$string, $matches);
//preg_match_all("/(\<[
\\n\\r\\t]{0,}style[^>]*\>|\<[^>]*[\\n\\r\\t]{1,}style[^>]*\>){1}([^<]*<([^(\/>)]*(\/[^(t>)]){0,1}(\/t[^(r>)]){0,1})*>)*(\<[
\\n\\r\\t]{0,}\/tr[^>]*\>|\<[^>]*[\\n\\r\\t]{1,}\/style[^>]*\>){1}/i",
$html, $Matches);

foreach($matches[0] as $div) {
echo $div;

}

?>
This should point you in the right direction...

<?php
preg_match('/<style(.*)?>(.*)?<\/style>/', $html, $match);
print $match[2];
?>

Sep 8 '06 #2
I'm afraid this did not work, but I will keep trying to tweak it and se
if i can get it to work.

Regular expressions have to be one of the hardest parts of php!!! i
really really dohn't like it!
Noodle wrote:
ameshkin wrote:
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

$url = "http://www.anyurlwithstyletags.com";
$html = file_get_contents($url);

//preg_match_all("/<style.*style>/", $html, $matches);
preg_match_all("/<div.*div>/", $html, $matches);
//preg_match_all("/[\s]*[<style>][\s]*[\w]*[\s]*[<\\style>][\s]*/",
$string, $matches);
//preg_match_all("/(\<[
\\n\\r\\t]{0,}style[^>]*\>|\<[^>]*[\\n\\r\\t]{1,}style[^>]*\>){1}([^<]*<([^(\/>)]*(\/[^(t>)]){0,1}(\/t[^(r>)]){0,1})*>)*(\<[
\\n\\r\\t]{0,}\/tr[^>]*\>|\<[^>]*[\\n\\r\\t]{1,}\/style[^>]*\>){1}/i",
$html, $Matches);

foreach($matches[0] as $div) {
echo $div;

}

?>

This should point you in the right direction...

<?php
preg_match('/<style(.*)?>(.*)?<\/style>/', $html, $match);
print $match[2];
?>
Sep 9 '06 #3

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

Similar topics

0
by: SteveJ | last post by:
All, Can someone help me solve the next step. First of all let me say I'm new to php. I pieced the following code together from samples I found on the net and a book I bought called PHP...
0
by: Yaseen | last post by:
Hi, I am trying to copy the content of word document to a DHMLEditor. When i copy and paste the content in the editor it shows only a placeholder for the images and it take different tags...
14
by: j1c | last post by:
How can I remove the content in between tags? I have a page that has several custom tags: <!--tag:1--> Content 1 <!--/tag:1--> <br> <!--tag:2--> Content 2 <!--/tag:2--> <br> <!--tag:3--> Content 3...
10
by: clintonG | last post by:
Can somebody direct me to documents or source that supports the use of collapsible content that is collapsed by default when the page is loaded? The secondary objective would of course be...
10
by: mark4 | last post by:
Hello, Are there any utilities to help me extract Content from HTML ? I'd like to store this data in a database. The HTML consists of about 10,000 files with a total size of about 160 Mb....
14
by: laurence | last post by:
I am implementing a comprehensive image-map generator utility, so have been studying W3C HTML 4.01 Specification (http://www.w3.org/TR/html4/struct/objects.html#h-13.6) on image maps (among other...
9
by: gregmcmullinjr | last post by:
Hello, I am new to the concept of XSL and am looking for some assistance. Take the following XML document: <binder> <author>Greg</author> <notes> <time>11:45</time>
1
by: Alberto Sartori | last post by:
Hello, I have a html text with custom tags which looks like html comment, such: "text text text <p>text</ptext test test text text text <p>text</ptext test test <!-- @MyTag@ -->extract...
0
by: peter pilsl | last post by:
For feeding the content of an xml-file to a search-indexer I need to remove all tags and extract the plaintext out of a xml-file. I use the null-xls-stylesheet <?xml version="1.0"?>...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.