Bonjour,
Je veux traduire du code html en bbcode. J'utilise une classe que j'ai adapté. Elle fonctionne dans les deux sens (bbcode -> html et html vers bbcode) sauf pour l'alignement qui ne fonctionne pas dans le sens html vers bbcode. La fonction htmlToBbcode() ne convertit pas le html en bbcode. Le html reste inchangé, c'est à dire tel qu'il a été traduit en sens inverse (exemple: <div style="text-align: center;"><!-- du texte --></div>).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
 
/** 
 * Class for BBCodes-tags
 * and converting them to HTML-tags 
 *
 * @author Thomas SCELLIER <contact@tomirisweb.fr>
 * @version 1.0
 * Last changes : 15/07/2016
 *
 * @translated in english and adapted by MP 
 */
 
namespace BBCode;
 
class BBCode
{
	/**
	 * List of BBCode-tags
	 * @var <array>
	 */
	protected static $bbcodeTags;
 
	/**
	 * List of HTML-tags
	 * @var <array>
	 */
	protected static $htmlTags;
 
 
	/**
	 * Convert content with 
	 * BBCode-tags into HTML-content
	 *
	 * @param <string> Content
	 * @return <string> Converted content
	 */
	public static function bbcodeToHtml($content)
	{
		$content = stripslashes(nl2br($content,false));
		BBCode::initializeBbcodeToHtml();
		for ($i = 0 ; $i < count(self::$bbcodeTags) ; $i++) {
			$content = preg_replace(self::$bbcodeTags[$i], self::$htmlTags[$i], $content);
		}
		return $content;
	}
 
	/**
	 * Convert content with 
	 * HTML-tags into BBCode-tags
	 *
	 * @param <string> Content
	 * @return <string> Converted content
	 */
	public static function htmlToBbcode($content)
	{
		$content = stripslashes($content);
		BBCode::initializeHtmlToBbcode();
		for ($i = 0 ; $i < count(self::$htmlTags) ; $i++) {
			$content = preg_replace(self::$htmlTags[$i], self::$bbcodeTags[$i], $content);
		}
		return $content;
	}
 
	/**
	 * Initializes pattern and replacement values
	 * for converting BBCode to HTML
	 */
	protected static function initializeBbcodeToHtml()
	{
		self::$bbcodeTags = array('#\[h([0-6])](.+)\[/h([0-6])]#isU',
									'#\[p](.+)\[/p]#isU',
									'#\[hr]#isU',
									'#\[i](.+)\[/i]#isU',
									'#\[b](.+)\[/b]#isU',
									'#\[u](.+)\[/u]#isU',
									'#\[s](.+)\[/s]#isU',
									'#\[sup](.+)\[/sup]#isU',
									'#\[sub](.+)\[/sub]#isU',
									'#\[left](.+)\[/left]#isU',
									'#\[center](.+)\[/center]#isU',
									'#\[right](.+)\[/right]#isU',
									);
 
		self::$htmlTags = array('<h$1>$2</h$1>',
									'<p>$1</p>',
									'<hr>',
									'<em>$1</em>',
									'<strong>$1</strong>',
									'<span style="text-decoration:underline;">$1</span>',
									'<span style="text-decoration: line-through;">$1</span>',
									'<sup>$1</sup>',
									'<sub>$1</sub>',
									'<div style="text-align: left;">$1</div>',
									'<div style="text-align: center;">$1</div>',
									'<div style="text-align: right;">$1</div>',
									);
	}
 
	/**
	 * Initializes pattern and replacement values
	 * for converting HTML to BBCode
	 */
	protected static function initializeHtmlToBbcode()
	{
		self::$htmlTags = array('#\<h([1-6])>(.+)\</h([1-6])>#isU',
									'#\<p>(.+)\</p>#isU',
									'#<br ?/?>#isU',
									'#<hr ?/?>#isU',
									'#\<em>(.+)\</em>#isU',
									'#\<strong>(.+)\</strong>#isU',
									'#\<sup>(.+)\</sup>#isU',
									'#\<sub>(.+)\</sub>#isU',
									'#\<div style="text-align: left;">(.+)</div>#isU',
									'#\<div style="text-align: center;">(.+)</div>#isU',
									'#\<div style="text-align: right;">(.+)</div>#isU',
									);
 
		self::$bbcodeTags = array('[h$1]$2[/h$1]',
									'[p]$1[/p]',
									'',
									'[hr]',
									'[i]$1[/i]',
									'[b]$1[/b]',
									'[sup]$1[/sup]',
									'[sub]$1[/sub]',
									'[left]$1[/left]',
									'[center]$1[/center]',
									'[right]$1[/right]',
									);
	}
//}
 
	/**
	 * Clean repeated or unwanted <br> or <br />
	 */
	public static function cleanHtmlBr($string)
	{
		$eol	= PHP_EOL;
		$string	= preg_replace("#(<br ?/?>{$eol}){2,}#", "<br>{$eol}", $string);
		$string	= preg_replace("#(</?h[0-6]>|</?p>|</?hr>)<br>#","$1",$string);
		return $string;
	}
}
?>