$s = str_replace('#', '#', $s);
- $search = array(" \n", "\n ");
- $replace = array("\n", "\n");
- do {
- $oldtext = $s;
- $s = str_replace($search, $replace, $s);
- } while ($oldtext != $s);
-
- $s = str_replace("\n\n", '<br>', $s);
-
$s = html2bbcode($s);
// protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
/**
* @file include/html2bbcode.php
* @brief Converter for HTML to BBCode
- *
+ *
* Made by: ike@piratenpartei.de
* Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
* https://github.com/annando/Syncom
return($replace);
}
-function _replace_code_cb($m){
- return "<code>".str_replace("\n","<br>\n",$m[1]). "</code>";
-}
-
function html2bbcode($message)
{
$message = str_replace("\r", "", $message);
- $message = preg_replace_callback("|<pre><code>([^<]*)</code></pre>|ism", "_replace_code_cb", $message);
+ // Removing code blocks before the whitespace removal processing below
+ $codeblocks = [];
+ $message = preg_replace_callback('#<pre><code(?: class="([^"]*)")?>(.*)</code></pre>#iUs',
+ function ($matches) use (&$codeblocks) {
+ $return = '[codeblock-' . count($codeblocks) . ']';
+
+ $prefix = '[code]';
+ if ($matches[1] != '') {
+ $prefix = '[code=' . $matches[1] . ']';
+ }
+ $codeblocks[] = $prefix . $matches[2] . '[/code]';
+ return $return;
+ }
+ , $message);
$message = str_replace(array(
"<li><p>",
node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]');
node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]');
- node2bbcode($doc, 'code', array(), '[code]', '[/code]');
node2bbcode($doc, 'key', array(), '[code]', '[/code]');
$message = $doc->saveHTML();
// Handling Yahoo style of mails
$message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message);
- return(trim($message));
+ // Restore code blocks
+ $message = preg_replace_callback('#\[codeblock-([0-9]+)\]#iU',
+ function ($matches) use ($codeblocks) {
+ $return = '';
+ if (isset($codeblocks[intval($matches[1])])) {
+ $return = $codeblocks[$matches[1]];
+ }
+ return $return;
+ }
+ , $message);
+
+ $message = trim($message);
+
+ return $message;
}
?>