. */ // Will format a text with
in a semantic way class BlockFormat { var $lines = array(); var $paragraph = false; var $block = false; var $blockContent = ''; var $newtext = ''; var $blockElements = '(h[1-6]|ol|ul|pre|blockquote|p|dl|object|table|div)'; var $processInBlocks = array('blockquote', 'div'); function init($text) { $text = str_replace("\r", '', $text); $text = explode("\n", trim($text)); $this->lines = $text; } function openBlock($tag) { $this->block = true; $this->newtext.= '<'.$tag.">"; } function closeBlock($tag) { $this->block = false; if (in_array($tag, $this->processInBlocks)) { $bf = new BlockFormat; $this->blockContent = $bf->Process($this->blockContent); } $this->newtext.= $this->blockContent; $this->newtext.= "".$tag.">\n\n"; $this->blockContent = ''; } function openParagraph() { $this->paragraph = true; $this->newtext.= "
\n "; } function closeParagraph() { $this->paragraph = false; $this->newtext.= "\n
\n\n"; } function append($text) { if ($this->block) $this->blockContent .= $text . "\n"; elseif (!empty($text)) $this->newtext.= $text . "\n"; } function Process($text) { $this->init($text); foreach($this->lines as $i=>$line) { $line = trim($line); if (empty($line)) { if ($this->paragraph) { $this->closeParagraph(); } else { $this->append("\n"); } continue; } elseif (preg_match('/<('.$this->blockElements.'[^>]*)>/', $line, $match)) { if ($this->paragraph) { $this->closeParagraph(); } $this->openBlock($match[1]); $line = str_replace($match[0], '', $line); } else { if (!$this->paragraph && !$this->block) { $this->openParagraph(); } } if (preg_match('/<\/'.$this->blockElements.'>/', $line, $match) && $this->block) { $close_block = $match[1]; $line = str_replace($match[0], '', $line); } if ($this->paragraph && !empty($this->lines[$i+1])) { $line.= "