Advertisement
PifyZ

Classe PHP utile pour générer facilement du code HTML

Feb 20th, 2012
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Html {
  2.     /*
  3.      * Permet de générer facilement de l'HTML
  4.      *
  5.      * @auteur    PifyZ
  6.      * @version   1.0 20/02/2012
  7.     */
  8.  
  9.     private $html = '';
  10.     private $inline = array('br', 'hr', 'img', 'input');
  11.     private $balisesOuverte = array();
  12.  
  13.     public function __construct() {
  14.         /* Vide */
  15.     }
  16.  
  17.     public function __call($balise, $attributs) {
  18.         $attributs = implode('|', $attributs);
  19.         $attributs = explode('|', $attributs);
  20.  
  21.         $return = '<' . $balise;
  22.  
  23.         foreach ($attributs AS $attribut) :
  24.             $pregMatch1 = '#(.[^:]+):(.+)#';
  25.             $pregReplace1 = ' $1="$2"';
  26.  
  27.             $pregMatch2 = '#texte:(.+)#';
  28.             $pregReplace2 = '$1';
  29.  
  30.  
  31.             if (preg_match($pregMatch1, $attribut) AND !preg_match($pregMatch2, $attribut)) :
  32.                 $return .= preg_replace($pregMatch1, $pregReplace1, $attribut);
  33.             endif;
  34.  
  35.             $texte = (preg_match($pregMatch2, $attribut)) ? preg_replace($pregMatch2, $pregReplace2, $attribut) : '';
  36.         endforeach;
  37.  
  38.         if (in_array($balise, $this->inline)) :
  39.             $return .= ' />';
  40.         else :
  41.             $this->balisesOuverte[] = $balise;
  42.             $return .= '>' . $texte;
  43.         endif;
  44.  
  45.         $this->html .= $return;
  46.  
  47.         return $this;
  48.     }
  49.  
  50.     public function texte($texte) {
  51.         $this->html .= $texte;
  52.  
  53.         return $this;
  54.     }
  55.  
  56.     public function BBCode($texte) {
  57.         $return = $texte;
  58.  
  59.         $return = preg_replace_callback('#\[\[\=\=(.+)\=\=\]\]#isU', array($this, 'protection'), $return);
  60.  
  61.         $return = preg_replace('#\[lien=(http://(.+))\|(.+)\]#isU', '<a href="$1">$3</a>', $return);
  62.         $return = preg_replace('#\[lien=(http://(.+))\\]#isU', '<a href="$1">$1</a>', $return);
  63.  
  64.         $return = preg_replace('#\[p=(center|left|right|justify)\](.+)\[/p\]#isU', '<p class="$1">$2</p>', $return);
  65.  
  66.         $return = preg_replace('#\*\*(.+)\*\*#isU', '<span class="gras">$1</span>', $return);
  67.         $return = preg_replace('#\-\-(.+)\-\-#isU', '<span class="surligne">$1</span>', $return);
  68.         $return = preg_replace('#\_\_(.+)\_\_#isU', '<span class="souligneDessous">$1</span>', $return);
  69.         $return = preg_replace('#\`\`(.+)\`\`#isU', '<span class="souligneDessus">$1</span>', $return);
  70.         $return = preg_replace('#\,\,(.+)\,\,#isU', '<span class="petitesMajuscules">$1</span>', $return);
  71.         // $return = preg_replace('#\/\/(.+)\/\/#isU', '<span class="italique">$1</span>', $return);
  72.  
  73.         $this->html .= $return;
  74.  
  75.         return $this;
  76.     }
  77.  
  78.     private function protection($captures) {
  79.         $return = nl2br(htmlentities($captures[1]));
  80.  
  81.         return $return;
  82.     }
  83.  
  84.     public function fin() {
  85.         $this->html .= '</' . array_pop($this->balisesOuverte) . '>';
  86.  
  87.         return $this;
  88.     }
  89.  
  90.     public function debug() {
  91.         var_dump($this->html);
  92.         var_dump($this->balisesOuverte);
  93.     }
  94.  
  95.     public function __toString() {
  96.         return $this->html;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement