Jump to content
MakeWebGames

Parsing CSS - Best Method?


Recommended Posts

Posted

Hey all,

I'm attempting to parse through a HTML file and split the file up accordingly (CSS goes in one section, images goes in another, etc).

I managed to get to a good point where I am getting the CSS data using PHP's DOMDocument class; however, I'm hitting a roadblock once I get the data.

This data may have some image URLs in them (background image), so I would like to be able to update this with the "proper" URL of where it will be once this "process" completes.

This is where I'm at:

 

$htmldoc = new DOMDocument();
       $htmldoc->loadHTML($uhtml);
       $cssstuff = $htmldoc->getElementsByTagName('style');
       if($cssstuff <> "") { // check if there are any elements
           foreach($cssstuff as $css) {
               $cssdata = $css->nodeValue;
          }
       }

 

As you can see, I have extracted the CSS data into the $cssdata variable (as I am going to put this into a CSS file); however, I'm stuck on how to update an image URL in that CSS from here.

Any help would be greatly appreciated.

Thanks!

~G7470

Posted

[uSER=64687]Dominion[/uSER] has the right idea. Essentially, the user will be inputting HTML, and I will be taking that, splitting it apart, and saving each piece separately throughout the file system.

I did manage to find a way to get this to work; however, what I don't know is that if this is the best method for doing so with PHP and JS at my disposal.

I'm just displaying the CSS I got for now, but I will be writing it to a file a little bit later. Here it is semi-completed:

 

$htmldoc = new DOMDocument();
       $htmldoc->loadHTML($_POST['shtml']);
       // make directories
       mkdir($_POST['name']);
       mkdir($_POST['name'] . "/images");
       mkdir($_POST['name'] . "/css");
       $cssstuff = $htmldoc->getElementsByTagName('style');
           foreach($cssstuff as $css) {
               $cssdata = $css->nodeValue;
               $cssdataarr = explode(";", $cssdata); // split CSS line by line
               $num = count($cssdataarr);
               $count = 1;
               foreach($cssdataarr as $cssinfo){
                   $getpos = strpos($cssinfo, "url"); // find URLs
                   if($getpos !== false) { // If URL exists, split string, copy image, and change URL
                       $string1 = substr($cssinfo, 0, $getpos);
                       $string2 = substr($cssinfo, $getpos+4);
                       copy("/sheets/{$string2}", "/{$_POST['name']}/{$string2}");
                       $newcssdata .= $string1 . " url(../" . $string2 . ";<br />";
                   }
                   else {
                       if($num == $count) {
                           $newcssdata .= $cssinfo . "<br />";
                       }
                       else {
                           $newcssdata .= $cssinfo . ";<br />";
                       }
                   }
                   $count++;
               }
           }
           echo $newcssdata;

 

~G7470

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...