]*[^/>])?)>!si","\\1/>",$sample_html); }; function make_attr_value($attr, $html) { return preg_replace("#(<[^>]*\s){$attr}(\s|>|/>)#si","\\1{$attr}=\"{$attr}\"\\2",$html); }; function mk_open_tag_regexp($tag) { return "<\s*{$tag}(\s+[^>]*)?>"; }; function mk_close_tag_regexp($tag) { return "<\s*/\s*{$tag}\s*>"; }; function process_html($html) { $open = mk_open_tag_regexp("html"); $close = mk_close_tag_regexp("html"); if (!preg_match("#{$open}#is",$html)) { $html = "".$html; }; /** * Let's check if there's more than one tags inside the page text * If there are, remove everything except the first one and content between the first and second */ while (preg_match("#{$open}(.*?){$open}#is", $html)) { $html = preg_replace("#{$open}(.*?){$open}#is", "\\2", $html); }; if (!preg_match("#{$close}#is", $html)) { $html = $html.""; }; // PHP 5.2.0 compatilibty issue // preg_replace may accidentally return NULL on large files not matching this $html = preg_replace("#.*({$open})#is","\\1",$html); // PHP 5.2.0 compatilibty issue // preg_replace may accidentally return NULL on large files not matching this // Cut off all data before and after 'html' tag; unless we'll do it, // the XML parser will die violently $html = preg_replace("#^.*.*$#is","",$html); return $html; } function process_head($html) { $open = mk_open_tag_regexp("head"); $close = mk_close_tag_regexp("head"); $ohtml = mk_open_tag_regexp("html"); $chtml = mk_close_tag_regexp("html"); $obody = mk_open_tag_regexp("body"); if (!preg_match("#{$open}#is",$html)) { $html = preg_replace("#({$ohtml})(.*)({$obody})#is","\\1
\\3\\4",$html); } elseif (!preg_match("#{$close}#is",$html)) { if (preg_match("#{$obody}#is",$html)) { $html = preg_replace("#({$obody})#is","\\1",$html); } else { $html = preg_replace("#({$chtml})#is","\\1",$html); }; }; return $html; } function process_body($html) { $open = mk_open_tag_regexp("body"); $close = mk_close_tag_regexp("body"); $ohtml = mk_open_tag_regexp("html"); $chtml = mk_close_tag_regexp("html"); $chead = mk_close_tag_regexp("head"); if (!preg_match("#{$open}#is",$html)) { if (preg_match("#{$chead}#is",$html)) { $html = preg_replace("#({$chead})#is","\\1",$html); } else { $html = preg_replace("#({$ohtml})#is","\\1",$html); }; }; if (!preg_match("#{$close}#is",$html)) { $html = preg_replace("#({$chtml})#is","\\1",$html); }; // Now check is there any data between and . $html = preg_replace("#({$chead})(.+)({$open})#is","\\1\\3\\2",$html); // Check if there's any data between and $html = preg_replace("#({$close})(.+)({$chtml})#is","\\2\\1\\3",$html); return $html; } // Hmmm. May be we'll just write SAX parser on PHP? ;-) function fix_tags($html) { $result = ""; $tag_stack = array(); // these corrections can simplify the regexp used to parse tags // remove whitespaces before '/' and between '/' and '>' in autoclosing tags $html = preg_replace("#\s*/\s*>#is","/>",$html); // remove whitespaces between '<', '/' and first tag letter in closing tags $html = preg_replace("#<\s*/\s*#is","",$html); // remove whitespaces between '<' and first tag letter $html = preg_replace("#<\s+#is","<",$html); while (preg_match("#(.*?)(<([a-z\d]+)[^>]*/>|<([a-z\d]+)[^>]*(?|([a-z\d]+)[^>]*>)#is",$html,$matches)) { $result .= $matches[1]; $html = substr($html, strlen($matches[0])); // Closing tag if (isset($matches[5])) { $tag = $matches[5]; if ($tag == $tag_stack[0]) { // Matched the last opening tag (normal state) // Just pop opening tag from the stack array_shift($tag_stack); $result .= $matches[2]; } elseif (array_search($tag, $tag_stack)) { // We'll never should close 'table' tag such way, so let's check if any 'tables' found on the stack $no_critical_tags = !array_search('table',$tag_stack); if (!$no_critical_tags) { $no_critical_tags = (array_search('table',$tag_stack) >= array_search($tag, $tag_stack)); }; if ($no_critical_tags) { // Corresponding opening tag exist on the stack (somewhere deep) // Note that we can forget about 0 value returned by array_search, becaus it is handled by previous 'if' // Insert a set of closing tags for all non-matching tags $i = 0; while ($tag_stack[$i] != $tag) { $result .= "{$tag_stack[$i]}> "; $i++; }; // close current tag $result .= "{$tag_stack[$i]}> "; // remove it from the stack array_splice($tag_stack, $i, 1); // if this tag is not "critical", reopen "run-off" tags $no_reopen_tags = array("tr","td","table","marquee","body","html"); if (array_search($tag, $no_reopen_tags) === false) { while ($i > 0) { $i--; $result .= "<{$tag_stack[$i]}> "; }; } else { array_splice($tag_stack, 0, $i); }; }; } else { // No such tag found on the stack, just remove it (do nothing in out case, as we have to explicitly // add things to result }; } elseif (isset($matches[4])) { // Opening tag $tag = $matches[4]; array_unshift($tag_stack, $tag); $result .= $matches[2]; } else { // Autoclosing tag; do nothing specific $result .= $matches[2]; }; }; // Close all tags left while (count($tag_stack) > 0) { $tag = array_shift($tag_stack); $result .= "".$tag.">"; } return $result; } /** * This function adds quotes to attribute values; it attribute values already have quotes, no changes are made */ function quote_attrs($html) { while (preg_match("!(<[^>]*)\s([^=>]+)=([^'\"\r\n >]+)([\r\n >])!si",$html, $matches)) { $html = preg_replace("#(<[^>]*)\s([^=>]+)=([^'\"\r\n >]+)([\r\n >])#si","\\1 \\2='\\3'\\4",$html); }; return $html; }; function escape_attr_value_entities($html) { $html = str_replace("<","<",$html); $html = str_replace(">",">",$html); // Replace all character references by their decimal codes process_character_references($html); $html = escape_amp($html); return $html; } /** * Updates attribute values: if there's any unescaped <, > or & symbols inside an attribute value, * replaces them with corresponding entity. Also note that & should not be escaped if it is already the part * of entity reference * * @param String $html source HTML code * @return String updated HTML code */ function escape_attrs_entities($html) { $result = ""; // Regular expression may be described as follows: // (<[^>]*) - something starting with < (i.e. tag name and, probably, some attribute name/values pairs // \s([^\s=>]+)= - space after "something", followed by attribute name (which may contain anything except spaces, = and > signs // (['\"])([^\3]*?)\3 - quoted attribute value; (@todo won't work with escaped quotes inside value, by the way). while (preg_match("#^(.*)(<[^>]*)\s([^\s=>]+)=(['\"])([^\\4]*?)\\4(.*)$#si", $html, $matches)) { $new_value = escape_attr_value_entities($matches[5]); $result .= $matches[1].$matches[2]." ".$matches[3]."=".$matches[4].$new_value.$matches[4]; $html = $matches[6]; }; return $result.$html; }; function fix_attrs_spaces(&$html) { while (preg_match("#(<[^>]*)\s([^\s=>]+)=\"([^\"]*?)\"([^\s])#si", $html)) { $html = preg_replace("#(<[^>]*)\s([^\s=>]+)=\"([^\"]*?)\"([^\s])#si","\\1 \\2=\"\\3\" \\4",$html); }; while (preg_match("#(<[^>]*)\s([^\s=>]+)='([^']*?)'([^\s])#si", $html)) { $html = preg_replace("#(<[^>]*)\s([^\s=>]+)='([^']*?)'([^\s])#si","\\1 \\2='\\3' \\4",$html); }; } function fix_attrs_tag($tag) { if (preg_match("#(<)(.*?)(/\s*>)#is",$tag, $matches)) { $prefix = $matches[1]; $suffix = $matches[3]; $content = $matches[2]; } elseif (preg_match("#(<)(.*?)(>)#is",$tag, $matches)) { $prefix = $matches[1]; $suffix = $matches[3]; $content = $matches[2]; } else { return; }; if (preg_match("#^\s*(\w+)\s*(.*)\s*/\s*\$#is", $content, $matches)) { $tagname = $matches[1]; $raw_attrs = isset($matches[2]) ? $matches[2] : ""; } elseif (preg_match("#^\s*(\w+)\s*(.*)\$#is", $content, $matches)) { $tagname = $matches[1]; $raw_attrs = isset($matches[2]) ? $matches[2] : ""; } else { // A strange tag occurred; just remove everything $tagname = ""; $raw_attrs = ""; }; $attrs = array(); while (!empty($raw_attrs)) { if (preg_match("#^\s*(\w+?)\s*=\s*\"(.*?)\"(.*)$#is",$raw_attrs,$matches)) { $attr = strtolower($matches[1]); $value = $matches[2]; if (!isset($attrs[$attr])) { $attrs[$attr] = $value; }; $raw_attrs = $matches[3]; } elseif (preg_match("#^\s*(\w+?)\s*=\s*'(.*?)'(.*)$#is",$raw_attrs,$matches)) { $attr = strtolower($matches[1]); $value = $matches[2]; if (!isset($attrs[$attr])) { $attrs[$attr] = $value; }; $raw_attrs = $matches[3]; } elseif (preg_match("#^\s*(\w+?)=(\w+)(.*)$#is",$raw_attrs,$matches)) { $attr = strtolower($matches[1]); $value = $matches[2]; if (!isset($attrs[$attr])) { $attrs[$attr] = $value; }; $raw_attrs = $matches[3]; } elseif (preg_match("#^\s*\S+\s+(.*)$#is",$raw_attrs,$matches)) { // Just a junk at the beginning; skip till the first space $raw_attrs = $matches[1]; } else { $raw_attrs = ""; }; }; $str = ""; foreach ($attrs as $key => $value) { // In theory, if the garbage have been found inside the attrs section, we could get // and invalid attribute name here; just ignore them in this case if (HTML2PS_XMLUtils::valid_attribute_name($key)) { if (strpos($value,'"') !== false) { $str .= " ".$key."='".$value."'"; } else { $str .= " ".$key."=\"".$value."\""; }; }; }; return $prefix.$tagname.$str.$suffix; } function fix_attrs($html) { $result = ""; while (preg_match("#^(.*?)(<[^/].*?>)#is",$html,$matches)) { $result .= $matches[1].fix_attrs_tag($matches[2]); $html = substr($html, strlen($matches[0])); }; return $result.$html; } function fix_closing_tags($html) { return preg_replace("#\s*(\w+).*?>#","\\1>",$html); } function process_pagebreak_commands(&$html) { $html = preg_replace("#<\?page-break>|#","