From f1153f94887a65958896afa7fc9deb783c9c9da5 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 10 May 2016 21:47:51 +0200 Subject: [PATCH] Initial and final version --- COPYING | 14 ++++++++++ README.md | 6 +++++ class.bbcode.php | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ index.php | 34 ++++++++++++++++++++++++ test.bbcode | 12 +++++++++ 5 files changed, 135 insertions(+) create mode 100644 COPYING create mode 100644 README.md create mode 100644 class.bbcode.php create mode 100644 index.php create mode 100644 test.bbcode diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..ee7d6a5 --- /dev/null +++ b/COPYING @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + 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. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..adf7281 --- /dev/null +++ b/README.md @@ -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. diff --git a/class.bbcode.php b/class.bbcode.php new file mode 100644 index 0000000..8308d85 --- /dev/null +++ b/class.bbcode.php @@ -0,0 +1,69 @@ +tags = array(); + $this->smileys = array(); + + $this->register_bbcode("b", FALSE, FALSE, FALSE, "\\1", NULL); + $this->register_bbcode("i", FALSE, FALSE, FALSE, "\\1", NULL); + $this->register_bbcode("u", FALSE, FALSE, FALSE, "\\1", NULL); + $this->register_bbcode("link", FALSE, TRUE, FALSE, "\\1", "\\2"); + $this->register_bbcode("url", FALSE, TRUE, FALSE, "\\1", "\\2"); + $this->register_bbcode("img", FALSE, TRUE, FALSE, "\"\"", "\"\\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"), "
\n", $text); + } + + if ($parse_smileys) { + foreach ($this->smileys as $smiley => $pic) { + $text = str_replace($smiley, "\"{$smiley}\"", $text); + } + } + + return $text; + } +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..c4f8ccf --- /dev/null +++ b/index.php @@ -0,0 +1,34 @@ + + + bbCode test + + + +register_smiley(":)", "smileys/smile.gif"); + echo $bbcode->decode(file_get_contents("test.bbcode")); +?> + + + diff --git a/test.bbcode b/test.bbcode new file mode 100644 index 0000000..82a8587 --- /dev/null +++ b/test.bbcode @@ -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 :) +hihi +[b]teszt[/b]