Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BBCode
- {
- /** @var string */
- private $string;
- /** @var string|NULL */
- private $formattedString = NULL;
- /**
- * @param string
- */
- public function __construct($string)
- {
- $this->string = $string;
- }
- /**
- * @return BBCode
- */
- public function format()
- {
- $str = $this->string;
- // first change HTML tags to entities
- $str = htmlSpecialChars($str);
- // header
- $str = preg_replace("#\[h\](.*)\[\/h\]#", "<h2>$1</h2>", $str);
- // bold
- $str = preg_replace("#\[b\](.*)\[\/b\]#", "<b>$1</b>", $str);
- // italic
- $str = preg_replace("#\[i\](.*)\[\/i\]#", "<i>$1</i>", $str);
- // underline
- $str = preg_replace("#\[u\](.*)\[\/u\]#", "<u>$1</u>", $str);
- // strike
- $str = preg_replace("#\[s\](.*)\[\/s\]#", "<s>$1</s>", $str);
- // super
- $str = preg_replace("#\[sub\](.*)\[\/sub\]#", "<sub>$1</sub>", $str);
- // sub
- $str = preg_replace("#\[sup\](.*)\[\/sup\]#", "<sup>$1</sup>", $str);
- // center align
- $str = preg_replace("#\[center\](.*)\[\/center\]#", "<div style='text-align:center;'>$1</div>", $str);
- // color [color #ff0000]
- $str = preg_replace("#\[color \#([a-fA-F0-9]{3,6})\](.*)\[\/color\]#", "<span style='color:#$1;'>$2</span>", $str);
- // background color [background color #ff0000]
- $str = preg_replace("#\[background color \#([a-fA-F0-9]{3,6})\](.*)\[\/background color\]#", "<span style='background-color:#$1;'>$2</span>", $str);
- // text size [size 25]
- $str = preg_replace("#\[size ([0-9]{1,2})\](.*)\[\/size\]#", "<span style='font-size:$1;'>$2</span>", $str);
- // link to player
- $str = preg_replace("#\[player ([0-9]*)\](.*)\[\/player\]#", "<a href='player.php?id=$1'>$2</a>", $str);
- // link to tribe
- $str = preg_replace("#\[tribe ([0-9]*)\](.*)\[\/tribe\]#", "<a href='tribe.php?id=$1'>$2</a>", $str);
- // resources
- $str = str_replace("[silver]", "<img src='silver.png' />", $str);
- $str = str_replace("[gold]", "<img src='gold.png' />", $str);
- $str = str_replace("[wood]", "<img src='wood.png' />", $str);
- $str = str_replace("[stone]", "<img src='stone.png' />", $str);
- $str = str_replace("[iron]", "<img src='iron.png' />", $str);
- // line breaks
- $str = str_replace("\n", "<br />", $str);
- $this->formattedString = $str;
- return $this;
- }
- /**
- * @return string
- */
- public function get()
- {
- return $this->formattedString;
- }
- /**
- * @return string
- */
- public function getRaw()
- {
- return $this->string;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement