transforms and > to >, no typography additions.
* @param boolean $allowHtml Set this to trim whitespace and disable all HTML
* @return string Formatted text
*/
function toHtml ($text=null, $bare=false, $allowHtml=false)
{
if (empty($text) && empty($this->text))
{
return false;
}
$text = $text? $text: $this->text;
// trim whitespace and disable all HTML
if ($allowHtml)
{
$text = trim($text);
}
else
{
$text = str_replace('<', '<', str_replace('>', '>', trim($text)));
}
if (!$bare)
{
// multi-paragraph functions
$text = preg_replace('#(?:[\n]{0,2})"""(.*)"""(?:[\n]{0,2})#s', "\n\n%BLOCKQUOTE%\n\n\\1\n\n%ENDBLOCKQUOTE%\n\n", $text);
$text = preg_replace('#(?:[\n]{0,2})===(.*)===(?:[\n]{0,2})#s', "\n\n%CENTER%\n\n\\1\n\n%ENDCENTER%\n\n", $text);
}
// pre-parse newlines
$text = preg_replace("#\r\n#", "\n", $text);
$text = preg_replace("#[\n]{2,}#", "%PARAGRAPH%", $text);
$text = preg_replace('#[\n]{1}#', "%LINEBREAK%", $text);
// split into paragraphs and parse
$out = '';
foreach (split('%PARAGRAPH%', $text) as $line)
{
if ($line)
{
if (!$bare)
{
// pre-parse links
$links = array();
$regs = null;
if (preg_match_all('#\[([^\[]{4,})\]#', $line, $regs))
{
foreach ($regs[1] as $reg)
{
$links[] = $reg;
$line = str_replace("[{$reg}]",'%LINK'.(count($links)-1).'%', $line);
}
}
// MAIN TEXT FUNCTIONS
// bold
$line = ereg_replace("\*([^\*]*)\*", "\\1", $line);
// italic
$line = ereg_replace("_([^_]*)_", "\\1", $line);
}
// entities
$line = str_replace(' - ', ' – ', $line);
$line = str_replace(' -- ', ' — ', $line);
$line = str_replace('(C)', '©', $line);
$line = str_replace('(R)', '®', $line);
$line = str_replace('(TM)', '™', $line);
// guess e-mails
$emails = null;
if (preg_match_all("#([_A-Za-z0-9+-+]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#", $line, $emails))
{
foreach ($emails[1] as $email)
{
$line = str_replace($email, "{$email}", $line);
}
}
if (!$bare)
{
// guess links
$urls = null;
if (preg_match_all("#((?:http|https|ftp|nntp)://[^ ]+)#", $line, $urls))
{
foreach ($urls[1] as $url)
{
$line = str_replace($url, "{$url}", $line);
}
}
if (preg_match_all("#(www\.[^\n\%\ ]+[^\n\%\,\.\ ])#", $line, $urls))
{
foreach ($urls[1] as $url)
{
$line = str_replace($url, "{$url}", $line);
}
}
// re-parse links
if (count($links))
{
for ($ii=0; $ii
\n", "
{$line}
\n"); } } if (!$bare) { // re-parse multilines $out = str_replace('%BLOCKQUOTE%
', "", $out); $out = str_replace('", $out); $out = str_replace('%ENDBLOCKQUOTE%
', "
%CENTER%
', "%ENDCENTER%
', "