Advertisement
Guest User

Untitled

a guest
Aug 1st, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. class BBCode
  2. {
  3.  
  4. /** @var string */
  5. private $string;
  6.  
  7. /** @var string|NULL */
  8. private $formattedString = NULL;
  9.  
  10. /**
  11. * @param string
  12. */
  13. public function __construct($string)
  14. {
  15. $this->string = $string;
  16. }
  17.  
  18.  
  19. /**
  20. * @return BBCode
  21. */
  22. public function format()
  23. {
  24. $str = $this->string;
  25.  
  26. // first change HTML tags to entities
  27. $str = htmlSpecialChars($str);
  28.  
  29. // header
  30. $str = preg_replace("#\[h\](.*)\[\/h\]#", "<h2>$1</h2>", $str);
  31.  
  32. // bold
  33. $str = preg_replace("#\[b\](.*)\[\/b\]#", "<b>$1</b>", $str);
  34.  
  35. // italic
  36. $str = preg_replace("#\[i\](.*)\[\/i\]#", "<i>$1</i>", $str);
  37.  
  38. // underline
  39. $str = preg_replace("#\[u\](.*)\[\/u\]#", "<u>$1</u>", $str);
  40.  
  41. // strike
  42. $str = preg_replace("#\[s\](.*)\[\/s\]#", "<s>$1</s>", $str);
  43.  
  44. // super
  45. $str = preg_replace("#\[sub\](.*)\[\/sub\]#", "<sub>$1</sub>", $str);
  46.  
  47. // sub
  48. $str = preg_replace("#\[sup\](.*)\[\/sup\]#", "<sup>$1</sup>", $str);
  49.  
  50. // center align
  51. $str = preg_replace("#\[center\](.*)\[\/center\]#", "<div style='text-align:center;'>$1</div>", $str);
  52.  
  53. // color [color #ff0000]
  54. $str = preg_replace("#\[color \#([a-fA-F0-9]{3,6})\](.*)\[\/color\]#", "<span style='color:#$1;'>$2</span>", $str);
  55.  
  56. // background color [background color #ff0000]
  57. $str = preg_replace("#\[background color \#([a-fA-F0-9]{3,6})\](.*)\[\/background color\]#", "<span style='background-color:#$1;'>$2</span>", $str);
  58.  
  59. // text size [size 25]
  60. $str = preg_replace("#\[size ([0-9]{1,2})\](.*)\[\/size\]#", "<span style='font-size:$1;'>$2</span>", $str);
  61.  
  62. // link to player
  63. $str = preg_replace("#\[player ([0-9]*)\](.*)\[\/player\]#", "<a href='player.php?id=$1'>$2</a>", $str);
  64.  
  65. // link to tribe
  66. $str = preg_replace("#\[tribe ([0-9]*)\](.*)\[\/tribe\]#", "<a href='tribe.php?id=$1'>$2</a>", $str);
  67.  
  68. // resources
  69. $str = str_replace("[silver]", "<img src='silver.png' />", $str);
  70. $str = str_replace("[gold]", "<img src='gold.png' />", $str);
  71. $str = str_replace("[wood]", "<img src='wood.png' />", $str);
  72. $str = str_replace("[stone]", "<img src='stone.png' />", $str);
  73. $str = str_replace("[iron]", "<img src='iron.png' />", $str);
  74.  
  75. // line breaks
  76. $str = str_replace("\n", "<br />", $str);
  77.  
  78. $this->formattedString = $str;
  79.  
  80. return $this;
  81. }
  82.  
  83.  
  84. /**
  85. * @return string
  86. */
  87. public function get()
  88. {
  89. return $this->formattedString;
  90. }
  91.  
  92.  
  93. /**
  94. * @return string
  95. */
  96. public function getRaw()
  97. {
  98. return $this->string;
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement