Codice PHP:
<?php
/**
*
*/
function regexB($text) {
return preg_replace("/\[b\](.*?)\[\/b\]/", "<b>\\1</b>", $text);
}
/**
*
*/
function regexU($text) {
return preg_replace("/\[u\](.*?)\[\/u\]/", "<u>\\1</u>", $text);
}
/**
*
*/
function regexI($text) {
return preg_replace("/\[i\](.*?)\[\/i\]/", "<i>\\1</i>", $text);
}
/**
*
*/
function regexImg($text) {
return preg_replace("/\[img\](http|https|ftp){1}(:\/\/){1}(.*?)\[\/img\]/", "<img src=\"\\1\\2\\3\" />", $text);
}
/**
*
*/
function regexColor($text, $colors = array("red", "blue", "purple", "orange", "yellow", "gray", "green")) {
return preg_replace("/\[color=(" . implode("|", $colors) . ")\](.+?)\[\/color\]/is", "<span style='color:\\1;line-height:100%'>\\2</span>", $text);
}
/**
*
*/
function regexFont($text, $fonts = array("arial", "times", "courier", "impact", "geneva", "optima")) {
return preg_replace("/\[font=(" . implode("|", $fonts) . ")\](.*?)\[\/font\]/is", "<span style='font-family:\\1'>\\2</span>", $text);
}
/**
*
*/
define("FORMAT_EM", "em");
define("FORMAT_PX", "px");
define("FORMAT_PM", "pm");
function regexSize($text, $sizes = array(1, 2, 3, 4, 5, 6, 7), $format = FORMAT_PM) {
return preg_replace("/\[size=(" . implode("|", $sizes) . ")\](.+?)\[\/size\]/is", "<span style='font-size:\\1{$format};line-height:100%'>\\2</span>", $text);
}
/**
*
*/
function regexEmail($text) {
$text = preg_replace( "/\[email\](.*?)\[\/email\]/i" , "<a href='mailto:\\1'>\\1</a>", $text);
$text = preg_replace( "/\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]/is" , "<a href='mailto:\\1'>\\2</a>", $text);
return $text;
}
/**
*
*/
function regexLink($text) {
$html = preg_replace("/\[url\](http|https|ftp){1}(:\/\/){1}(.*?)\[\/url\]/is" , "<a href=\"\\1\\2\\3\">\\1\\2\\3</a>" , $text);
$html = preg_replace( "/\[url\s*=\s*(http|https|ftp){1}(:\/\/){1}(.*?)\s*\](.*?)\[\/url\]/is" , "<a href=\"\\1\\2\\3\">\\4</a>" , $text);
return $text;
}
/**
*
*/
function regexQuote($text, $openTag = "<div name='quote'>", $closedTag = "</div>", $writtenBy = "Written by <strong>%s</strong><br />") {
$text = preg_replace("/\[quote](.*?)\[\/quote\]/is" , "{$openTag}\\1{$closedTag}" , $text);
$text = preg_replace("/\[quote\s*=\s*(.*?){1}\s*\](.*?)\[\/quote\]/is" , "{$openTag}" . sprintf($writtenBy, htmlspecialchars("\\1")) . "\\2{$closedTag}" , $text);
return $text;
}
/**
*
*/
define("OPEN_UL" , "<ul>");
define("CLOSED_UL" , "</ul>");
function regexUL() {
return array(
"(\
[list\])",
"(\[\*\].*?\[\/\*\])",
"(\[\/list\])"
);
}
/**
*
*/
define("OPEN_LI" , "<li>");
define("CLOSED_LI" , "</li>");
function regexLI() {
return array(
//
"(\[\*\].*?\[\/\*\])",
//
"\[\*\](.*?)\[\/\*\]"
);
}
/**
*
*/
define("OPEN_CODE" , "<pre>");
define("CLOSED_CODE", "</pre>");
function regexCode() {
return array(
"(\[code\])",
"(\[\/code\])",
);
}
/**
*
*/
function parseCode($text) {
$regexUL = regexUL();
$regexLI = regexLI();
$regexCode = regexCode();
$regexp = "/" . implode("|", $regexUL) . "|" . implode("|", $regexCode) . "|{$regexLI[0]}/";
$code = preg_split($regexp, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$html = "";
$bb_ul = $bb_code = false;
while ($parsed = htmlspecialchars(array_shift($code))) {
if (preg_match("/{$regexCode[0]}/", $parsed) && !$bb_code) {
$html .= OPEN_CODE;
$bb_code = true;
}
elseif (preg_match("/{$regexCode[1]}/", $parsed) && $bb_code) {
$html .= CLOSED_CODE;
$bb_code = false;
}
elseif (preg_match("/{$regexUL[0]}/", $parsed) && !$bb_code) {
$html .= OPEN_UL;
$bb_ul = true;
}
elseif (preg_match("/{$regexLI[1]}/", $parsed, $return) && !$bb_code && $bb_ul) {
$html .= "<li>{$return[1]}</li>";
}
elseif (preg_match("/{$regexUL[2]}/", $parsed) && !$bb_code && $bb_ul) {
$html .= CLOSED_UL;
$bb_ul = false;
}
else {
if (!$bb_code) {
$parsed = regexB($parsed);
$parsed = regexU($parsed);
$parsed = regexI($parsed);
$parsed = regexImg($parsed);
$parsed = regexColor($parsed);
$parsed = regexFont($parsed);
$parsed = regexSize($parsed);
$parsed = regexEmail($parsed);
$parsed = regexLink($parsed);
$parsed = regexQuote($parsed);
}
$html .= $parsed;
}
}
return $html;
}
?>