Initial and final version

This commit is contained in:
Gergely Polonkai 2016-05-10 21:47:51 +02:00
commit f1153f9488
5 changed files with 135 additions and 0 deletions

14
COPYING Normal file
View File

@ -0,0 +1,14 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.

6
README.md Normal file
View File

@ -0,0 +1,6 @@
# bbCode class for PHP
This is a very old PHP class I wrote for parsing bbCode, a then very popular
formatting markup. It is for PHP3, which is very old now.
Feel free to copy, modify or blast it.

69
class.bbcode.php Normal file
View File

@ -0,0 +1,69 @@
<?php
class bbcode {
var $tags;
var $smileys;
function bbcode()
{
$this->tags = array();
$this->smileys = array();
$this->register_bbcode("b", FALSE, FALSE, FALSE, "<span class=\"b\">\\1</span>", NULL);
$this->register_bbcode("i", FALSE, FALSE, FALSE, "<span class=\"i\">\\1</span>", NULL);
$this->register_bbcode("u", FALSE, FALSE, FALSE, "<span class=\"u\">\\1</span>", NULL);
$this->register_bbcode("link", FALSE, TRUE, FALSE, "<a href=\"\\1\">\\1</a>", "<a href=\"\\1\">\\2</a>");
$this->register_bbcode("url", FALSE, TRUE, FALSE, "<a href=\"\\1\">\\1</a>", "<a href=\"\\1\">\\2</a>");
$this->register_bbcode("img", FALSE, TRUE, FALSE, "<img src=\"\\1\" alt=\"\" />", "<img src=\"\\1\" alt=\"\\2\" />");
}
function register_bbcode($tagname, $noclose, $can_have_param, $must_have_param, $replacement, $param_replacement)
{
$this->tags[$tagname] = array(
"tagname" => $tagname,
"noclose" => $noclose,
"can_have_param" => $can_have_param,
"must_have_param" => $must_have_param,
"replacement" => $replacement,
"param_replacement" => $param_replacement
);
}
function register_smiley($smiley, $pic)
{
$this->smileys[$smiley] = $pic;
}
function decode($text, $parse_bbcode = TRUE, $parse_smileys = TRUE)
{
$text = strip_tags($text);
if ($parse_bbcode) {
$continue = TRUE;
while ($continue) {
foreach ($this->tags as $tagname => $definition) {
$continue = FALSE;
$num = 0;
$pattern_parm = "\\[{$definition["tagname"]}=([^]]+)\\]([^[]+)\\[\\/{$definition["tagname"]}\\]";
$pattern_noparm = "\\[{$definition["tagname"]}\\]([^[]+)\\[\\/{$definition["tagname"]}\\]";
$text = preg_replace("/{$pattern_noparm}/i", $definition["replacement"], $text, -1, $num);
$text = preg_replace("/{$pattern_parm}/i", $definition["param_replacement"], $text, -1, $num);
if ($num > 0) {
$continue = TRUE;
}
}
}
$text = str_replace(array("\r", "\n", "\r\n"), "<br />\n", $text);
}
if ($parse_smileys) {
foreach ($this->smileys as $smiley => $pic) {
$text = str_replace($smiley, "<img src=\"{$pic}\" alt=\"{$smiley}\">", $text);
}
}
return $text;
}
}

34
index.php Normal file
View File

@ -0,0 +1,34 @@
<html>
<head>
<title>bbCode test</title>
<style>
span.b {
font-weight: bold;
}
span.i {
font-style: italic;
}
span.u {
text-decoration: underline;
}
a {
text-decoration: none;
font-weight: bold;
color: green;
}
</style>
</head>
<body>
<?php
include("class.bbcode.php");
$bbcode = new bbcode;
$bbcode->register_smiley(":)", "smileys/smile.gif");
echo $bbcode->decode(file_get_contents("test.bbcode"));
?>
</body>
</html>

12
test.bbcode Normal file
View File

@ -0,0 +1,12 @@
[b]Bold[/b]
[i]Italic[/i]
[u]Underline[/u]
[b][i]Bold Italic[/i][/b]
[i][b]Italic Bold[/b][/i]
[link]http://w00d5t0ck.info/[/link]
[link=http://w00d5t0ck.info/]W00d5t0ck[/link]
[img]http://w00d5t0ck.info/images/fej.png[/img]
[img=http://w00d5t0ck.info/images/fej.png]Fej[/img]
<strong>strong :)</strong>
<a href="hehe"><img src="haha" alt="hihi" /></a>
[b]teszt[/b]