A fork of potaka/bbcode to handle future maintenance
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # BB code parser for php 7.0+
  2. Builds:
  3. [![SensioLabsInsight](https://insight.sensiolabs.com/projects/054684fa-c2d1-4cc3-8905-d4a797961c22/big.png)](https://insight.sensiolabs.com/projects/054684fa-c2d1-4cc3-8905-d4a797961c22)
  4. [![Build Status](https://travis-ci.org/angelk/bbCode.svg?branch=jenkins-integrati)](https://travis-ci.org/angelk/bbCode)
  5. There are two parts - tokenizer and parser.
  6. Tokenization - convert string to tokens.
  7. BbCodeParser - convert bbCodeTokens to html (+ some validations)
  8. # The Easy Way
  9. ```php
  10. $tokenizer = new \Potaka\BbCode\Tokenizer\Tokenizer();
  11. $bbText = '[b]bold[/b]text[u]under[i]line[/i][/u]';
  12. $tokenized = $tokenizer->tokenize($text);
  13. $factory = new \Potaka\BbCode\Factory();
  14. $bbcode = $factory->getFullBbCode();
  15. $html = $bbcode->format($tokenized);
  16. ```
  17. The value if `html` is
  18. ```html
  19. <b>bold</b>text<u>under<i>line</i></u>
  20. ```
  21. # Installation
  22. ```
  23. composer require potaka/bbcode
  24. ```
  25. # Internal explanation
  26. ## Tokenization
  27. For example
  28. ```
  29. $bbText = '[b]bold[/b]text[u]under[i]line[/i][/u]';
  30. ```
  31. Will be tokenized to
  32. ```yml
  33. Tag:
  34. type: null,
  35. tags:
  36. tag1:
  37. type: b
  38. tags:
  39. tag1:
  40. type: null
  41. text: bold
  42. tag2:
  43. type: null
  44. text: text
  45. tag3:
  46. type: u
  47. tags:
  48. tag1:
  49. type: null,
  50. text: under
  51. tag2:
  52. type: i
  53. tags:
  54. tag1:
  55. type: null,
  56. text: line
  57. ```
  58. ## Tokenized to html
  59. You need to have valid `bb code tags`. Build in tags are available in https://github.com/angelk/bbCode/tree/master/src/Potaka/BbCode/Tag
  60. Building the parser:
  61. ```php
  62. use Potaka\BbCode;
  63. use Potaka\BbCode\Tokenizer;
  64. use Potaka\BbCode\Tag\Bold;
  65. use Potaka\BbCode\Tag\Underline;
  66. use Potaka\BbCode\Tag\Italic;
  67. use Potaka\BbCode\Tag\Link;
  68. $bbcode = new BbCode();
  69. ```
  70. Lets add the `b` code
  71. ```
  72. $bold = new Bold();
  73. $bbcode->addTag($bold);
  74. ```
  75. Lets format the token from above
  76. ```
  77. $tokenizer = new Tokenizer();
  78. $tokenized = $tokenizer->tokenize($bbText);
  79. $bbcode->format($tokenized);
  80. ```
  81. will return
  82. ```
  83. <b>bold</b>text[u]under[i]line[/i][/u]
  84. ```
  85. `u` and `i` are not formated cuz tags are not added.
  86. Lets add em.
  87. ```
  88. $underline = new Underline();
  89. $bbcode->addTag($underline);
  90. $italic = new Italic();
  91. $bbcode->addTag($italic);
  92. ```
  93. Test again
  94. ```
  95. $bbcode->format($tokenized);
  96. ```
  97. Result:
  98. ```html
  99. <b>bold</b>text<u>under[i]line[/i]</u>
  100. ```
  101. Why `i` is not converted? Cuz `u` doesn't allow child tag of type `i`. Lets fix this
  102. ```
  103. $bbcode->addAllowedChildTag($underline, $italic);
  104. ```
  105. Everything should work now!
  106. Whats the purpose of this allowing? Imagine you have link `[url]http://google.bg[/url]`.
  107. What if someone try to put link in link `[url=http://google.bg]google.[url=http://gmail.com]bg[/url][/url]`?
  108. This will generate
  109. ```html
  110. <a href="http://google.bg">
  111. google.<a href="http://gmail.com">bg</a>
  112. </a>
  113. ```
  114. This html is invalid. It could even provide [xss](https://en.wikipedia.org/wiki/Cross-site_scripting). This is why you should not allow `url` inside `url`.