Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function htmlToArray($html) {
- // move forward processing text into array in serial fashion
- $elements =
- array(
- 'parent_id' => array(),
- 'tag_index' => array(),
- 'tag_name' => array(),
- 'tag_id' => array(),
- 'tag_class' => array(),
- 'tag_style' => array(),
- 'tag_type' => array(),
- 'tag_src' => array(),
- 'tag_href' => array(),
- 'tag_attr' => array(),
- 'tag_text' => array(),
- 'tag_rel' => array(),
- 'tag_content' => array(),
- 'tag_property' => array(),
- );
- $html = trim($html);
- $len = strlen($html);
- $numClose = array('tag' => array());
- $numOpen = array('tag' => array());
- $level = 0;
- $index[0] = 0;
- $pIDs[0] = 0;
- for($i = 0; $i < $len; $i++) {
- preg_match('/(?:^[^>]+?)>/i', $html, $m);
- if(empty($m[0])) {
- break;
- }
- $tagString = $m[0];
- if(strpos($tagString, "</") === 0) { # if it's a close tag
- $i--; # don't count this as a new tag
- $level--; # move level back one
- array_pop($index); # remove the last index count
- array_pop($pIDs); # remove the last index count
- if(empty($numClose[$elements['tag_name'][$i]])) {
- $numClose[$elements['tag_name'][$i]] = 0;
- }
- else {
- $numClose[$elements['tag_name'][$i]]++;
- }
- $html = trim(substr($html, strlen($tagString)));
- if(strpos($html, '<') > 0) {
- # text content between any tags (not necessarily just open and close)
- $pos = strpos($html, '<');
- if(!empty($elements['tag_text'][$i-1])) {
- $elements['tag_text'][$i-1] .= substr($html, 0, $pos);
- }
- else {
- $elements['tag_text'][$i-1] = substr($html, 0, $pos);
- }
- // process text into elements array
- $html = trim(substr($html, $pos));
- }
- }
- else if(strpos($tagString, '<') === 0) {
- // parse current tag and enter into array
- $elements = self::parseHtmlTag($tagString, $elements);
- $tag_name = $elements['tag_name'][$i];
- $tLen = strlen($tag_name);
- $selfClose = false;
- $tagTest = array('hr', 'br', 'img', 'input', 'link', 'meta', '!doctype');
- for($j = 0; !empty($tagTest[$j]); $j++) {
- if($tag_name == mb_strtolower($tagTest[$j])) {
- $selfClose = true;
- }
- }
- $elements['parent_id'][] = $pIDs[count($pIDs)-1];
- $elements['tag_index'][] = $index[count($index)-1];
- $index[count($index)-1]++;
- if(!$selfClose) {
- $tempClose = substr($html, $i, $tLen+3);
- if($tempClose != "</$tag_name>") {
- if(empty($numOpen[$tag_name])) {
- $numOpen[$tag_name] = 0;
- }
- else {
- $numOpen[$tag_name]++;
- }
- $level++;
- $pIDs[] = $i;
- $index[] = 0;
- }
- }
- if(!empty($numClose[$tag_name]) && !empty($numOpen[$tag_name]) && $numClose[$tag_name] > $numOpen[$tag_name]) {
- // oopsies
- }
- $html = trim(substr($html, strlen($tagString)));
- }
- else if(strpos($tagString, '<') > 0) {
- $i--;
- # text content between any tags (not necessarily just open and close)
- $pos = strpos($tagString, '<');
- $elements['tag_text'][$i] = substr($tagString, 0, $pos);
- // process text into elements array
- $html = trim(substr($html, $pos));
- }
- else {
- break;
- }
- }
- return $elements;
- }
- public static function parseHtmlTag($tag, $array=array()) {
- $tagString = $tag_name = $tag_id = $tag_class = $tag_style = $tag_type = $tag_src = $tag_href = $tag_attr = $tag_rel = $tag_content = $tag_property = '';
- $tagString = trim($tag, '><');
- preg_match('/([!a-z0-9]+)/i', $tagString, $m);
- $tag_name = $m[1];
- $tagString = substr($tagString, strlen($tag_name));
- if(stripos($tagString, 'id=')) {
- preg_match('/id=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_id = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- if(stripos($tagString, 'class=')) {
- preg_match('/class=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_class = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- if(stripos($tagString, 'style=')) {
- preg_match('/style=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_style = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- if(stripos($tagString, 'type=')) {
- preg_match('/type=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_type = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- if(stripos($tagString, 'src=')) {
- preg_match('/src=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_src = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- if(stripos($tagString, 'href=')) {
- preg_match('/href=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_href = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- if(stripos($tagString, 'rel=')) {
- preg_match('/rel=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_rel = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- if(stripos($tagString, 'content=')) {
- preg_match('/content=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_content = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- if(stripos($tagString, 'property=')) {
- preg_match('/property=(\'|")([^\1]+)\1/i', $tagString, $m);
- if(!empty($m[2])) {
- $tag_property = $m[2];
- $tagString = str_replace($m[0], '', $tagString);
- }
- }
- $tagString = trim(preg_replace('/\s+/', ' ', $tagString));
- if(!empty($tagString)) {
- $tag_attr = trim($tagString, '<>');
- }
- $array['tag_name'][] = $tag_name;
- $array['tag_id'][] = $tag_id;
- $array['tag_class'][] = $tag_class;
- $array['tag_style'][] = $tag_style;
- $array['tag_type'][] = $tag_type;
- $array['tag_src'][] = $tag_src;
- $array['tag_href'][] = $tag_href;
- $array['tag_attr'][] = $tag_attr;
- $array['tag_rel'][] = $tag_rel;
- $array['tag_content'][] = $tag_content;
- $array['tag_property'][] = $tag_property;
- $array['tag_text'][] = '';
- return $array;
- }
Advertisement
Add Comment
Please, Sign In to add comment