Advertisement
arie_cristianD

override ContentTag.php

Nov 9th, 2023 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.53 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author Jegtheme
  4.  */
  5.  
  6. namespace JNews;
  7.  
  8. /**
  9.  * Class Tree Node
  10.  */
  11. Class ContentTag {
  12.     /**
  13.      * @var TreeNode
  14.      */
  15.     private $pointer;
  16.     private $root;
  17.     private static $content;
  18.     private $self_closing = array( 'img', 'embed', 'area', 'base', 'br', 'col', 'command', 'hr', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' );
  19.  
  20.     public function __construct( $content ) {
  21.         self::$content = $content;
  22.         $this->populate_tag();
  23.     }
  24.  
  25.     public function find( $tag, $number, $end=true ) {
  26.         if ( is_object( $this->pointer ) && is_array( $this->pointer->child ) ) {
  27.             foreach ( $this->pointer->child as $child ) {
  28.                 if ( $child->tag === $tag ) {
  29.                     $number --;
  30.                 }
  31.  
  32.                 if ( $number === 0 ) {
  33.                     return $child->end;
  34.                 }
  35.             }
  36.             if ( $end && is_object( end( $this->pointer->child ) ) ) {
  37.                 return end( $this->pointer->child )->end;
  38.             }
  39.             return 0;
  40.         }
  41.     }
  42.  
  43.     public function total( $tag ) {
  44.         $number = 0;
  45.  
  46.         if ( is_object( $this->pointer ) && is_array( $this->pointer->child ) ) {
  47.             foreach ( $this->pointer->child as $child ) {
  48.                 if ( $child->tag === $tag ) {
  49.                     $number ++;
  50.                 }
  51.             }
  52.         }
  53.  
  54.         return $number;
  55.     }
  56.  
  57.     protected function populate_tag() {
  58.         $this->pointer = new TreeNode();
  59.         $this->root    = $this->pointer;
  60.  
  61.         preg_match_all( '/<[^>]*>/im', self::$content, $matches, PREG_OFFSET_CAPTURE );
  62.  
  63.         foreach ( $matches[0] as $key => $match ) {
  64.             $tag = $this->get_tag( $match[0] );
  65.             if ( ! empty( $tag ) ) {
  66.                 if ( ! $this->is_closed_tag( $match[0] ) ) {
  67.                     $this->register_tag( $tag, $match[1] );
  68.                 } else {
  69.                     $this->reset_tag( $match[1] );
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     public static function get_content() {
  76.         return self::$content;
  77.     }
  78.  
  79.     protected function is_closed_tag( $tag ) {
  80.         return substr( $tag, 0, 2 ) === '</';
  81.     }
  82.  
  83.     protected function get_tag( $html ) {
  84.         $html = preg_replace("/<!--.*?-->/ms","",$html);
  85.         if ( ! empty( $html ) ) {
  86.             preg_match( '/<\/?([^\s^>]+)/', $html, $tag );
  87.  
  88.             return $tag[1];
  89.         }
  90.         return '';
  91.     }
  92.  
  93.     protected function register_tag( $tag, $start ) {
  94.         $this->pointer = $this->pointer === null ? $this->root : $this->pointer;
  95.         $this->pointer = $this->pointer->create_child( $tag, $start );
  96.  
  97.         // Reset tag if there is a self colsing tag html
  98.         if ( in_array( $tag, $this->self_closing ) ) {
  99.             $this->reset_tag( $start );
  100.         }
  101.     }
  102.  
  103.     protected function reset_tag( $end ) {
  104.         $this->pointer = $this->pointer === null ? $this->root : $this->pointer;
  105.         $this->pointer = $this->pointer->end_child( $end );
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement