<?php
$in_file="index.htm"; //this is the filename of the current dressup page
$out_file="index_fixed.html"; //this is the where the fixed one will go
$js_file="dressup.js";

$s = file_get_contents($in_file);

if($s===FALSE){
echo "Could not open file $out_file for reading.\n<br>";
exit();
}
/*first retrieve css stuff */
$text=explode("<style type=\"text/css\">",$s);
$s=explode("</style>",$text[1]);

$text=explode("#",$s[0]);

$itm=array();

for($i=0;$i<count($text);$i++){
$s=explode("{",$text[$i]);

$css=explode("\n",$s[1]);

$css[count($css)-2]="";
$css=implode("",$css);

$s[0]=substr($s[0],0,-1);
$itm[$s[0]]=$css;

}
/*now there is an associative array whose keys are div id's,
and whose values consist of style strings*/

$c=count($itm);
$k=array_keys($itm);
$s = file_get_contents($in_file);
/*now replace <div> tags with new ones which include style strings */
for($i=0;$i<$c;$i++){
$s=str_replace("<div id=\"" . $k[$i] . "\">", "<div id=\"" . $k[$i] . "\" style=\"". $itm[$k[$i]] . "\">",$s);

}

//java script

$j=explode("<script type=\"text/javascript\">",$s);
//whole file broken up into parts, separated by javascript decls.

for($i=0;$i<count($j);$i++){
$k=explode("</script>",$j[$i]); //each java script part
if(strpos($k[0],"Adobe Macromedia Software")!==FALSE){
$k[0]=file_get_contents($js_file); //replace new code with old code
}

$j[$i]=implode("</script>",$k);

}
$s=implode("<script type=\"text/javascript\">",$j);

if(file_put_contents($out_file, $s)===FALSE) {
echo "Could not open $out_file for writing.<br>\n";
}
else{
echo "Success";
}

 

?>