4 Converter for HTML to BBCode
5 Made by: ike@piratenpartei.de
6 Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
7 https://github.com/annando/Syncom
10 function node2bbcode(&$doc, $oldnode, $attributes, $startbb, $endbb)
13 $done = node2bbcodesub($doc, $oldnode, $attributes, $startbb, $endbb);
17 function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
19 $savestart = str_replace('$', '%', $startbb);
22 $xpath = new DomXPath($doc);
24 $list = $xpath->query("//".$oldnode);
25 foreach ($list as $oldNode) {
28 if ($oldNode->attributes->length)
29 foreach ($oldNode->attributes as $attribute)
30 $attr[$attribute->name] = $attribute->value;
34 $startbb = $savestart;
38 foreach ($attributes as $attribute => $value) {
40 $startbb = str_replace('%'.++$i, '$1', $startbb);
42 if (strpos('*'.$startbb, '$1') > 0) {
44 if ($replace and (@$attr[$attribute] != '')) {
46 $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
48 // If nothing could be changed
54 if (@$attr[$attribute] != $value)
60 $StartCode = $oldNode->ownerDocument->createTextNode($startbb);
61 $EndCode = $oldNode->ownerDocument->createTextNode($endbb);
63 $oldNode->parentNode->insertBefore($StartCode, $oldNode);
65 if ($oldNode->hasChildNodes()) {
66 foreach ($oldNode->childNodes as $child) {
67 $newNode = $child->cloneNode(true);
68 $oldNode->parentNode->insertBefore($newNode, $oldNode);
72 $oldNode->parentNode->insertBefore($EndCode, $oldNode);
73 $oldNode->parentNode->removeChild($oldNode);
79 function deletenode(&$doc, $node)
81 $xpath = new DomXPath($doc);
82 $list = $xpath->query("//".$node);
83 foreach ($list as $child)
84 $child->parentNode->removeChild($child);
87 function html2bbcode($message)
90 //$file = tempnam("/tmp/", "html");
91 //file_put_contents($file, $message);
93 $message = str_replace("\r", "", $message);
95 $message = str_replace(array(
104 $message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
105 $message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);
107 $doc = new DOMDocument();
108 $doc->preserveWhiteSpace = false;
110 $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
112 @$doc->loadHTML($message);
114 deletenode($doc, 'style');
115 deletenode($doc, 'head');
116 deletenode($doc, 'title');
117 deletenode($doc, 'meta');
118 deletenode($doc, 'xml');
119 deletenode($doc, 'removeme');
121 $xpath = new DomXPath($doc);
122 $list = $xpath->query("//pre");
123 foreach ($list as $node)
124 $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
126 $message = $doc->saveHTML();
127 $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
128 $message = preg_replace('= [\s]*=i', " ", $message);
129 @$doc->loadHTML($message);
131 node2bbcode($doc, 'html', array(), "", "");
132 node2bbcode($doc, 'body', array(), "", "");
134 // Outlook-Quote - Variant 1
135 node2bbcode($doc, 'p', array('class'=>'MsoNormal', 'style'=>'margin-left:35.4pt'), '[quote]', '[/quote]');
137 // Outlook-Quote - Variant 2
138 node2bbcode($doc, 'div', array('style'=>'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'), '[quote]', '[/quote]');
141 node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '[u]', '[/u]');
142 node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '[i]', '[/i]');
143 node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '[b]', '[/b]');
145 /*node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
146 node2bbcode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
147 node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
148 node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
149 node2bbcode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
150 node2bbcode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
151 node2bbcode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
154 //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
155 //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
156 //node2bbcode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');
158 node2bbcode($doc, 'span', array('style'=>'/.*color:\s*(.+?)[,;].*/'), '[color="$1"]', '[/color]');
159 //node2bbcode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
161 //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
162 //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
163 //node2bbcode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
165 node2bbcode($doc, 'strong', array(), '[b]', '[/b]');
166 node2bbcode($doc, 'em', array(), '[i]', '[/i]');
167 node2bbcode($doc, 'b', array(), '[b]', '[/b]');
168 node2bbcode($doc, 'i', array(), '[i]', '[/i]');
169 node2bbcode($doc, 'u', array(), '[u]', '[/u]');
171 node2bbcode($doc, 'big', array(), "[size=large]", "[/size]");
172 node2bbcode($doc, 'small', array(), "[size=small]", "[/size]");
174 node2bbcode($doc, 'blockquote', array(), '[quote]', '[/quote]');
176 node2bbcode($doc, 'br', array(), "\n", '');
178 node2bbcode($doc, 'p', array('class'=>'MsoNormal'), "\n", "");
179 node2bbcode($doc, 'div', array('class'=>'MsoNormal'), "\r", "");
181 node2bbcode($doc, 'span', array(), "", "");
183 node2bbcode($doc, 'span', array(), "", "");
184 node2bbcode($doc, 'pre', array(), "", "");
185 node2bbcode($doc, 'div', array(), "\r", "\r");
186 node2bbcode($doc, 'p', array(), "\n", "\n");
188 node2bbcode($doc, 'ul', array(), "[list]", "[/list]");
189 node2bbcode($doc, 'ol', array(), "[list=1]", "[/list]");
190 node2bbcode($doc, 'li', array(), "[*]", "");
192 node2bbcode($doc, 'hr', array(), "[hr]", "");
194 node2bbcode($doc, 'table', array(), "", "");
195 node2bbcode($doc, 'tr', array(), "\n", "");
196 node2bbcode($doc, 'td', array(), "\t", "");
197 //node2bbcode($doc, 'table', array(), "[table]", "[/table]");
198 //node2bbcode($doc, 'th', array(), "[th]", "[/th]");
199 //node2bbcode($doc, 'tr', array(), "[tr]", "[/tr]");
200 //node2bbcode($doc, 'td', array(), "[td]", "[/td]");
202 node2bbcode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
203 node2bbcode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
204 node2bbcode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
205 node2bbcode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
206 node2bbcode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
207 node2bbcode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
209 node2bbcode($doc, 'a', array('href'=>'/(.+)/'), '[url=$1]', '[/url]');
211 node2bbcode($doc, 'img', array('src'=>'/(.+)/', 'width'=>'/(\d+)/', 'height'=>'/(\d+)/'), '[img=$2x$3]$1', '[/img]');
212 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
215 node2bbcode($doc, 'video', array('src'=>'/(.+)/'), '[video]$1', '[/video]');
216 node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]');
217 node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]');
219 node2bbcode($doc, 'code', array(), '[code]', '[/code]');
221 $message = $doc->saveHTML();
223 // I'm removing something really disturbing
224 // Don't know exactly what it is
225 $message = str_replace(chr(194).chr(160), ' ', $message);
227 $message = str_replace(" ", " ", $message);
229 // removing multiple DIVs
230 $message = preg_replace('=\r *\r=i', "\n", $message);
231 $message = str_replace("\r", "\n", $message);
233 call_hooks('html2bbcode', $message);
235 $message = strip_tags($message);
237 $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
239 $message = str_replace(array("<"), array("<"), $message);
241 // remove quotes if they don't make sense
242 $message = preg_replace('=\[/quote\][\s]*\[quote\]=i', "\n", $message);
244 $message = preg_replace('=\[quote\]\s*=i', "[quote]", $message);
245 $message = preg_replace('=\s*\[/quote\]=i', "[/quote]", $message);
248 $oldmessage = $message;
249 $message = str_replace("\n \n", "\n\n", $message);
250 } while ($oldmessage != $message);
253 $oldmessage = $message;
254 $message = str_replace("\n\n\n", "\n\n", $message);
255 } while ($oldmessage != $message);
258 $oldmessage = $message;
259 $message = str_replace(array(
280 } while ($message != $oldmessage);
282 $message = str_replace(array('[b][b]', '[/b][/b]', '[i][i]', '[/i][/i]'),
283 array('[b]', '[/b]', '[i]', '[/i]'), $message);
285 // Handling Yahoo style of mails
286 $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
288 return(trim($message));