- while(preg_match('/<pre[^>]*>(.*)<\/pre>/ismU', $text, $matches))
- {
- $result = preg_replace($this->pre_search, $this->pre_replace, $matches[1]);
- $text = preg_replace('/<pre[^>]*>.*<\/pre>/ismU', '<div><br>' . $result . '<br></div>', $text, 1);
- }
+ // get the content of PRE element
+ while (preg_match('/<pre[^>]*>(.*)<\/pre>/ismU', $text, $matches)) {
+ // convert the content
+ $this->pre_content = sprintf('<div><br>%s<br></div>',
+ preg_replace($this->pre_search, $this->pre_replace, $matches[1]));
+ // replace the content (use callback because content can contain $0 variable)
+ $text = preg_replace_callback('/<pre[^>]*>.*<\/pre>/ismU',
+ array('html2text', '_preg_pre_callback'), $text, 1);
+ // free memory
+ $this->pre_content = '';
+ }
+ }
+
+ /**
+ * Helper function for BLOCKQUOTE body conversion.
+ *
+ * @param string HTML content
+ * @access private
+ */
+ function _convert_blockquotes(&$text)
+ {
+ if (preg_match_all('/<\/*blockquote[^>]*>/i', $text, $matches, PREG_OFFSET_CAPTURE)) {
+ $level = 0;
+ $diff = 0;
+ foreach ($matches[0] as $m) {
+ if ($m[0][0] == '<' && $m[0][1] == '/') {
+ $level--;
+ if ($level < 0) {
+ $level = 0; // malformed HTML: go to next blockquote
+ }
+ else if ($level > 0) {
+ // skip inner blockquote
+ }
+ else {
+ $end = $m[1];
+ $len = $end - $taglen - $start;
+ // Get blockquote content
+ $body = substr($text, $start + $taglen - $diff, $len);
+
+ // Set text width
+ $p_width = $this->width;
+ if ($this->width > 0) $this->width -= 2;
+ // Convert blockquote content
+ $body = trim($body);
+ $this->_converter($body);
+ // Add citation markers and create PRE block
+ $body = preg_replace('/((^|\n)>*)/', '\\1> ', trim($body));
+ $body = '<pre>' . htmlspecialchars($body) . '</pre>';
+ // Re-set text width
+ $this->width = $p_width;
+ // Replace content
+ $text = substr($text, 0, $start - $diff)
+ . $body . substr($text, $end + strlen($m[0]) - $diff);
+
+ $diff = $len + $taglen + strlen($m[0]) - strlen($body);
+ unset($body);
+ }
+ }
+ else {
+ if ($level == 0) {
+ $start = $m[1];
+ $taglen = strlen($m[0]);
+ }
+ $level ++;
+ }
+ }
+ }