djr33
02-15-2007, 06:40 AM
I wrote this code to parse BB code for a website I made.
It includes ,[u],[i],, and *
(*this is for a site talking about movies, so a spoiler is something that would give away part of the plot, so it hides that from being displayed. Easy enough to remove this from the code, though)
It also parses links, like http://, https://, mailto:, and ftp://.
(It doesn't yet parse emails by themselves.)
Emoticons are also easy.
For now I'll just include examples of :) and :(.
function badlink($link, $prefix) {
if ($prefix == "mailto:") {
if (strpos($link, "@") === FALSE || strpos($link, ".", (strpos($link, "@")+2)) === FALSE || substr_count($link, "@") > 1 || strpos($link, "@") == 0) {
return 1;
}
}
if (strpos($link, ".") == 0 || strpos($link, ".") == strlen($link) || (strpos($link, "/") < strpos($link, ".") && strpos($link, "/") !== FALSE)) {
return 1;
}
};
function setlinks($r, $prefix) {
if (substr($r, 0, strlen($prefix)) == $prefix) {
$r = "\n".$r;
}
$r = str_replace("<br>".$prefix, "<br>\n".$prefix, $r);
$r = str_replace(" ".$prefix, " \n".$prefix, $r);
while (strpos($r, "\n".$prefix) !== FALSE) {
list($r1, $r2) = explode("\n".$prefix, $r, 2);
if (strpos($r2, " ") === FALSE && strpos($r2, "<br>") === FALSE) {
if ($prefix != "mailto:") {
$target = ' target="_blank"';
}
else {
$target = "";
}
if (strpos($r2, ".") > 1 && strpos($r2, ".") < strlen($r2) && badlink($r2, $prefix) != 1) {
$r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a>';
}
else {
$r = $r1.$prefix.$r2;
}
}
else {
if (strpos($r2, " ") === FALSE || ( strpos($r2, " ") > strpos($r2, "<br>") && strpos($r2, "<br>") !== FALSE)) {
list($r2, $r3) = explode("<br>", $r2, 2);
if (badlink($r2, $prefix) != 1) {
$r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a><br>'.$r3;
}
else {
$r = $r1.$prefix.$r2.'<br>'.$r3;
}
}
else {
list($r2, $r3) = explode(" ", $r2, 2);
if (strpos($r2, ".") > 1 && strpos($r2, ".") < strlen($r2) && badlink($r2, $prefix) != 1) {
$r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a> '.$r3;
}
else {
$r = $r1.$prefix.$r2.' '.$r3;
}
}
}
}
return $r;
};
function bb($r) {
$r = trim($r);
$r = htmlentities($r);
$r = str_replace("\r\n","<br>",$r);
$r = str_replace("[b]","<b>",$r);
$r = str_replace("","</b>",$r);
$r = str_replace("","<i>",$r);
$r = str_replace("","</i>",$r);
$r = str_replace("","<u>",$r);
$r = str_replace("","</u>",$r);
$r = str_replace("",'[spoiler]<font color="#DDDDDD">',$r);
$r = str_replace("","</font>",$r);
//set s
while (strpos($r, "[link=") !== FALSE) {
list ($r1, $r2) = explode("[link=", $r, 2);
if (strpos($r2, "]") !== FALSE) {
list ($r2, $r3) = explode("]", $r2, 2);
if (strpos($r3, "") !== FALSE) {
list($r3, $r4) = explode("", $r3, 2);
$target = ' target="_blank"';
if (substr($r2, 0, 7) == "mailto:") {
$target = "";
}
$r = $r1.'<a href="'.$r2.'"'.$target.'>'.$r3.'</a>'.$r4;
}
else {
$r = $r1."[link\n=".$r2."]".$r3;
}
}
else {
$r = $r1."[link\n=".$r2;
}
}
$r = str_replace("[link\n=","[link=",$r);
////[link]
///default url link setting
$r = setlinks($r, "http://");
$r = setlinks($r, "https://");
$r = setlinks($r, "ftp://");
$r = setlinks($r, "mailto:");
////links
///emoticons
$r = str_replace(":)",'<img src="myhappyimg.gif">',$r);
$r = str_replace(":(",'<img src="mysadimg.gif">',$r);
//Repeat for more if desired.
///emoticons
$r = trim($r);
return $r;
}
Notes: The first function is used in the second and the second in the third, main function. So you don't want to change the order of these. Also, you cannot set a function within another because if it's called twice, an error will be generated.
To use... VERY simple:
$text = bb($text);
I don't claim this is perfect, but I think it works well enough. It doesn't check for tags left open, but that's just something that your users will need the common sense to deal with.
Note that using a <div> or a similar element around each instance of this when placed back into your page might be a good idea so that you don't have miscellaneous stray <b>s, etc., leaving your page open to be made bold throughout.
Also, this function is html injection SAFE. The BB codes generate html, but any html entered will be converted to html entities and rendered harmless.
And here's a page listing the functions:
http://thebrb.com/theater/index.php?act=markups&type=review
It includes ,[u],[i],, and *
(*this is for a site talking about movies, so a spoiler is something that would give away part of the plot, so it hides that from being displayed. Easy enough to remove this from the code, though)
It also parses links, like http://, https://, mailto:, and ftp://.
(It doesn't yet parse emails by themselves.)
Emoticons are also easy.
For now I'll just include examples of :) and :(.
function badlink($link, $prefix) {
if ($prefix == "mailto:") {
if (strpos($link, "@") === FALSE || strpos($link, ".", (strpos($link, "@")+2)) === FALSE || substr_count($link, "@") > 1 || strpos($link, "@") == 0) {
return 1;
}
}
if (strpos($link, ".") == 0 || strpos($link, ".") == strlen($link) || (strpos($link, "/") < strpos($link, ".") && strpos($link, "/") !== FALSE)) {
return 1;
}
};
function setlinks($r, $prefix) {
if (substr($r, 0, strlen($prefix)) == $prefix) {
$r = "\n".$r;
}
$r = str_replace("<br>".$prefix, "<br>\n".$prefix, $r);
$r = str_replace(" ".$prefix, " \n".$prefix, $r);
while (strpos($r, "\n".$prefix) !== FALSE) {
list($r1, $r2) = explode("\n".$prefix, $r, 2);
if (strpos($r2, " ") === FALSE && strpos($r2, "<br>") === FALSE) {
if ($prefix != "mailto:") {
$target = ' target="_blank"';
}
else {
$target = "";
}
if (strpos($r2, ".") > 1 && strpos($r2, ".") < strlen($r2) && badlink($r2, $prefix) != 1) {
$r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a>';
}
else {
$r = $r1.$prefix.$r2;
}
}
else {
if (strpos($r2, " ") === FALSE || ( strpos($r2, " ") > strpos($r2, "<br>") && strpos($r2, "<br>") !== FALSE)) {
list($r2, $r3) = explode("<br>", $r2, 2);
if (badlink($r2, $prefix) != 1) {
$r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a><br>'.$r3;
}
else {
$r = $r1.$prefix.$r2.'<br>'.$r3;
}
}
else {
list($r2, $r3) = explode(" ", $r2, 2);
if (strpos($r2, ".") > 1 && strpos($r2, ".") < strlen($r2) && badlink($r2, $prefix) != 1) {
$r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'>'.$prefix.$r2.'</a> '.$r3;
}
else {
$r = $r1.$prefix.$r2.' '.$r3;
}
}
}
}
return $r;
};
function bb($r) {
$r = trim($r);
$r = htmlentities($r);
$r = str_replace("\r\n","<br>",$r);
$r = str_replace("[b]","<b>",$r);
$r = str_replace("","</b>",$r);
$r = str_replace("","<i>",$r);
$r = str_replace("","</i>",$r);
$r = str_replace("","<u>",$r);
$r = str_replace("","</u>",$r);
$r = str_replace("",'[spoiler]<font color="#DDDDDD">',$r);
$r = str_replace("","</font>",$r);
//set s
while (strpos($r, "[link=") !== FALSE) {
list ($r1, $r2) = explode("[link=", $r, 2);
if (strpos($r2, "]") !== FALSE) {
list ($r2, $r3) = explode("]", $r2, 2);
if (strpos($r3, "") !== FALSE) {
list($r3, $r4) = explode("", $r3, 2);
$target = ' target="_blank"';
if (substr($r2, 0, 7) == "mailto:") {
$target = "";
}
$r = $r1.'<a href="'.$r2.'"'.$target.'>'.$r3.'</a>'.$r4;
}
else {
$r = $r1."[link\n=".$r2."]".$r3;
}
}
else {
$r = $r1."[link\n=".$r2;
}
}
$r = str_replace("[link\n=","[link=",$r);
////[link]
///default url link setting
$r = setlinks($r, "http://");
$r = setlinks($r, "https://");
$r = setlinks($r, "ftp://");
$r = setlinks($r, "mailto:");
////links
///emoticons
$r = str_replace(":)",'<img src="myhappyimg.gif">',$r);
$r = str_replace(":(",'<img src="mysadimg.gif">',$r);
//Repeat for more if desired.
///emoticons
$r = trim($r);
return $r;
}
Notes: The first function is used in the second and the second in the third, main function. So you don't want to change the order of these. Also, you cannot set a function within another because if it's called twice, an error will be generated.
To use... VERY simple:
$text = bb($text);
I don't claim this is perfect, but I think it works well enough. It doesn't check for tags left open, but that's just something that your users will need the common sense to deal with.
Note that using a <div> or a similar element around each instance of this when placed back into your page might be a good idea so that you don't have miscellaneous stray <b>s, etc., leaving your page open to be made bold throughout.
Also, this function is html injection SAFE. The BB codes generate html, but any html entered will be converted to html entities and rendered harmless.
And here's a page listing the functions:
http://thebrb.com/theater/index.php?act=markups&type=review