Connecting Tech Pros Worldwide Help | Site Map

Extract Content Between Style Tags with Preg and PHP

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 8th, 2006, 05:25 AM
ameshkin
Guest
 
Posts: n/a
Default 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;

}

?>


  #2  
Old September 8th, 2006, 08:25 AM
Noodle
Guest
 
Posts: n/a
Default Re: Extract Content Between Style Tags with Preg and PHP


ameshkin wrote:
Quote:
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];
?>

  #3  
Old September 9th, 2006, 05:25 AM
ameshkin
Guest
 
Posts: n/a
Default Re: Extract Content Between Style Tags with Preg and PHP

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:
Quote:
ameshkin wrote:
Quote:
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];
?>
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.