Helpful Information
 
 
Category: Coding tips & tutorials threads
Custom BB code function in PHP

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

Thanks! :D

No image tag?

Edit: Don't worry, will do it myself :p

Note: ... is identical in coding to the [link] tag. Just do the same
Also, you might want to verify that it is an image with imagesize() to see if it parses as such.

Nice script djr (I know you already showed me, but... anyways). I'm going to be implementing it soon.

Not bad, but you are very limited if you don't use regular expressions. They can be confusing, but are well with. As an example, I put together how to do this with regular expressions. It supports b,i,u,quote,s,list(and * ),url(both kinds),img,color,size,smilies,auto link urls,auto link email addresses, and converts newlines to <br> tags.
Test it: http://www.webtech101.com/uploads/dd/bb_code/index.php
See the code: http://www.webtech101.com/uploads/dd/bb_code/simple_bb_code.phps

Interesting. I never claimed regex are bad... just never dealt with learning them.
I'll take a look when I get the chance. (At the code, I mean... saw the demo.)

I think that el/strong should not be used... b=b, i=i... no?

Hmm...Maybe, but strong and em are always better than b/i which are presentational markup.

Not always. Or they wouldn't exist.

<b> and <i> are presentational... yes. If your design called for a specific type of look for the em or strong tags, then your user would be confused.
BB users would always expect bold from b and italics from i. In that case, it SHOULD dictacte the graphic look of the text.










privacy (GDPR)