G7470 Posted April 25, 2016 Share Posted April 25, 2016 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 Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted April 25, 2016 Share Posted April 25, 2016 I will admit, I am quite confused with what you mean. Any chance you can explain it a bit more? Quote Link to comment Share on other sites More sharing options...
Dominion Posted April 26, 2016 Share Posted April 26, 2016 So you're trying to get a html file imput by the user and everything from that split up by the system before being saved separately? Quote Link to comment Share on other sites More sharing options...
G7470 Posted April 26, 2016 Author Share Posted April 26, 2016 [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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.