doc = $text; $this->Init_Parse(); $this->Parse(); } public function Parse(){ $offset = 0; do{ $continue = true; $pos = strpos($this->doc,'<',$offset); //no more tags if( $pos === false ){ $continue = false; break; } //comment if( substr($this->doc,$pos,4) === ' * Does not support full sgml comments second comment --> * */ public function CommentContent(&$offset){ $this->doc = substr($this->doc,$offset); $offset = 0; $pos = strpos($this->doc,'-->'); if( $pos === false ){ $pos = strlen($this->doc); } $comment_content = substr($this->doc,0,$pos); $this->doc = substr($this->doc,$pos+3); $new_element = array(); $new_element['comment'] = $comment_content; $this->dom_array[] = $new_element; } public function NonHtmlContent(&$offset,$untill){ $this->doc = substr($this->doc,$offset); $offset = 0; $this->doc = $this->EscapeQuotes($this->doc); $full_length = strlen($this->doc); $untill_length = strlen($untill); do{ $continue = false; $end_string = false; $pos_quote1 = $this->strpos_min("'",$offset,$full_length); $pos_quote2 = $this->strpos_min('"',$offset,$full_length); $pos_scomment = $this->strpos_min('//',$offset,$full_length); $pos_mcomment = $this->strpos_min('/*',$offset,$full_length); $min_pos = min($pos_quote1, $pos_quote2, $pos_scomment, $pos_mcomment); $pos_close = strpos($this->doc,' if( ($pos_close !== false) && ($pos_close <= $min_pos) && (strtolower(substr($this->doc,$pos_close+2,$untill_length)) == $untill) ){ $offset = $pos_close; break; } // nothing else found if( $min_pos === $full_length ){ $offset = $full_length; break; } if( $min_pos === $pos_quote1 ){ $end_string = "'"; }elseif( $min_pos === $pos_quote2 ){ $end_string = '"'; }elseif( $min_pos === $pos_scomment ){ $end_string = "\n"; }elseif( $min_pos === $pos_mcomment ){ $end_string = '*/'; } $end_pos = strpos($this->doc,$end_string,$min_pos+1); if( $end_pos === false ){ $offset = $full_length; }else{ $offset = $full_length; $offset = $end_pos + strlen($end_string); $continue = true; } }while($continue); $code = substr($this->doc,0,$offset); $this->doc = substr($this->doc,$offset); $this->doc = $this->UnescapeQuotes($this->doc); $this->dom_array[] = $this->UnescapeQuotes($code); $offset = 0; } public function strpos_min($needle,$offset,$length){ $pos = strpos($this->doc,$needle,$offset); if( $pos === false ){ return $length; } return $pos; } public function EscapeQuotes($string){ $search = array('\\\\','\\\'','\\"'); $replace = array( $this->mark_double_slash, $this->mark_escaped_single, $this->mark_escaped_double); return str_replace($search, $replace, $string); } public function UnescapeQuotes($string){ $search = array( $this->mark_double_slash, $this->mark_escaped_single, $this->mark_escaped_double); $replace = array('\\\\','\\\'','\\"'); return str_replace($search, $replace, $string); } /* * Init * */ public function Init_Parse(){ $this->GetRandom(); $this->mark_double_slash = $this->GetMarker(); $this->mark_escaped_single = $this->GetMarker(); $this->mark_escaped_double = $this->GetMarker(); } public function GetRandom(){ do{ $this->random = dechex(mt_rand(0, 0x7fffff)); }while(strpos($this->doc,$this->random) !== false); } public function GetMarker(){ static $n = 0; return $this->random . sprintf('%08X', $n++); } }