SHOW:
|
|
- or go back to the newest paste.
1 | class Html { | |
2 | - | /** |
2 | + | /* |
3 | * Permet de générer facilement de l'HTML | |
4 | * | |
5 | - | * @author PifyZ |
5 | + | * @auteur PifyZ |
6 | - | * @version 1.0 19/02/2012 |
6 | + | * @version 1.0 20/02/2012 |
7 | - | **/ |
7 | + | */ |
8 | ||
9 | private $html = ''; | |
10 | - | private $inline = array('br', 'hr', 'img', 'input'); /* Ils ne sont pas tout listés (à changer au besoin) */ |
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 | - | } |
65 | + | |
66 | $return = preg_replace('#\*\*(.+)\*\*#isU', '<span class="gras">$1</span>', $return); | |
67 | - | /* Exemple d'utilisation (non testé) -> |
67 | + | $return = preg_replace('#\-\-(.+)\-\-#isU', '<span class="surligne">$1</span>', $return); |
68 | - | $html = new Html(); |
68 | + | $return = preg_replace('#\_\_(.+)\_\_#isU', '<span class="souligneDessous">$1</span>', $return); |
69 | - | $html->h1('texte:Titre de taille 1')->fin() |
69 | + | $return = preg_replace('#\`\`(.+)\`\`#isU', '<span class="souligneDessus">$1</span>', $return); |
70 | - | ->p('texte:Texte')->fin(); |
70 | + | $return = preg_replace('#\,\,(.+)\,\,#isU', '<span class="petitesMajuscules">$1</span>', $return); |
71 | - | echo $html; |
71 | + | // $return = preg_replace('#\/\/(.+)\/\/#isU', '<span class="italique">$1</span>', $return); |
72 | ||
73 | - | Ou encore (génère le même code) (non testé) -> |
73 | + | |
74 | - | $html = new Html(); |
74 | + | |
75 | - | $html->h1() |
75 | + | |
76 | - | ->texte('Titre de taille 1') |
76 | + | |
77 | - | ->fin() |
77 | + | |
78 | - | ->p() |
78 | + | private function protection($captures) { |
79 | - | ->texte('Texte') |
79 | + | $return = nl2br(htmlentities($captures[1])); |
80 | - | ->fin(); |
80 | + | |
81 | - | echo $html; |
81 | + | return $return; |
82 | - | */ |
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 | } |