PageRenderTime 70ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/www/caches/scripts/StringParcer/doc/en/chapter9.html

http://ds-pro-chat.googlecode.com/
HTML | 168 lines | 167 code | 0 blank | 1 comment | 0 complexity | afb3920b16d6500fd13a35df479ea2b3 MD5 | raw file
Possible License(s): MIT
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <meta http-equiv="Content-Language" content="en">
  6. <title>StringParser_BBCode class documentation</title>
  7. <meta name="author" content="Christian Seiler">
  8. <link rel="stylesheet" href="../css/print.css" type="text/css" media="print">
  9. <link rel="stylesheet" href="../css/screen.css" type="text/css" media="screen, projection">
  10. </head>
  11. <body id="doku">
  12. <div id="container">
  13. <h1><span><code>StringParser_<abbr title="Bulletin Board Code">BBCode</abbr></code> class</span> documentation</h1>
  14. <ul id="mainmenu">
  15. <li><a href="http://www.christian-seiler.de/projekte/php/bbcode/index_en.html">Project homepage</a></li>
  16. <li><a href="chapter1.html">Documentation</a></li>
  17. <li><a href="../phpdoc/index.html">PHPDOC Documentation</a></li>
  18. <li><a href="http://www.christian-seiler.de/projekte/php/bbcode/download_en.html">Download</a></li>
  19. <li><a href="../de/kapitel9.html">Diese Seite auf Deutsch</a></li>
  20. </ul>
  21. <ul id="menu">
  22. <li><a href="chapter1.html">1. Introduction</a>
  23. <ul>
  24. <li><a href="chapter1.html#general">1.1 General</a></li>
  25. <li><a href="chapter1.html#nesting">1.2 Nesting</a></li>
  26. <li><a href="chapter1.html#special">1.3 Special codes</a></li>
  27. </ul></li>
  28. <li><a href="chapter2.html">2. Defining own <abbr>BBCode</abbr></a>
  29. <ul>
  30. <li><a href="chapter2.html#including">2.1 Including the class</a></li>
  31. <li><a href="chapter2.html#first">2.2 The first code</a></li>
  32. <li><a href="chapter2.html#processing_types">2.3 Processing types</a></li>
  33. <li><a href="chapter2.html#parsing">2.4 Parsing text</a></li>
  34. </ul></li>
  35. <li><a href="chapter3.html">3. Parser functions</a>
  36. <ul>
  37. <li><a href="chapter3.html#task">3.1 Task of parser functions</a></li>
  38. <li><a href="chapter3.html#content_types">3.2 Relevance of content types</a></li>
  39. <li><a href="chapter3.html#registration">3.3 Registration of parser functions</a></li>
  40. </ul></li>
  41. <li><a href="chapter4.html">4. Callback functions</a>
  42. <ul>
  43. <li><a href="chapter4.html#processing_types">4.1 Processing types that need callback functions</a></li>
  44. <li><a href="chapter4.html#prototype">4.2 Prototype of a callback function</a></li>
  45. <li><a href="chapter4.html#example">4.3 Example for a callback function that replaces links</a></li>
  46. <li><a href="chapter4.html#revalidation">4.4 Revalidation on close tag occurrence</a></li>
  47. </ul></li>
  48. <li><a href="chapter5.html">5. Filters</a>
  49. <ul>
  50. <li><a href="chapter5.html#types">5.1 Filter types</a></li>
  51. <li><a href="chapter5.html#defining">5.2 Defining filters</a></li>
  52. </ul></li>
  53. <li><a href="chapter6.html">6. Flags to control the behaviour of the class</a>
  54. <ul>
  55. <li><a href="chapter6.html#general">6.1 General</a></li>
  56. <li><a href="chapter6.html#flags">6.2 List of all flags</a></li>
  57. <li><a href="chapter6.html#global">6.2 Global flags</a></li>
  58. </ul></li>
  59. <li><a href="chapter7.html">7. Limiting the amount of occurrences</a>
  60. <ul>
  61. <li><a href="chapter7.html#grouping">7.1 Grouping codes</a></li>
  62. <li><a href="chapter7.html#limits">7.2 Setting limits</a></li>
  63. </ul></li>
  64. <li><a href="chapter8.html">8. Paragraph handling</a>
  65. <ul>
  66. <li><a href="chapter8.html#general">8.1 General</a></li>
  67. <li><a href="chapter8.html#activating">8.2 Activating paragraph handling</a></li>
  68. <li><a href="chapter8.html#further">8.3 Further possibilities</a></li>
  69. </ul></li>
  70. <li><a href="chapter9.html">9. Examples</a>
  71. <ul>
  72. <li><a href="chapter9.html#example">9.1 Simple example</a></li>
  73. <!-- <li><a href="chapter9.html#complex">9.2 More complex example</a></li> -->
  74. </ul></li>
  75. <li><a href="chapter10.html">10. Miscellaneous</a>
  76. <ul>
  77. <li><a href="chapter10.html#faq">10.1 Frequently asked questions</a></li>
  78. <li><a href="chapter10.html#internals">10.2 Useful internals</a></li>
  79. </ul></li>
  80. </ul>
  81. <div id="content"><h2>9. Examples</h2>
  82. <h3 id="example"><a name="example">9.1 Example</a></h3>
  83. <p class="php"><code>// Unify line breaks of different operating systems<br>
  84. function convertlinebreaks ($text) {<br>
  85. &nbsp;&nbsp;&nbsp;&nbsp;return preg_replace ("/\015\012|\015|\012/", "\n", $text);<br>
  86. }<br>
  87. <br>
  88. // Remove everything but the newline charachter<br>
  89. function bbcode_stripcontents ($text) {<br>
  90. &nbsp;&nbsp;&nbsp;&nbsp;return preg_replace ("/[^\n]/", '', $text);<br>
  91. }<br>
  92. <br>
  93. function do_bbcode_url ($action, $attributes, $content, $params, $node_object) {<br>
  94. &nbsp;&nbsp;&nbsp;&nbsp;if (!isset ($attributes['default'])) {<br>
  95. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$url = $content;<br>
  96. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$text = htmlspecialchars ($content);<br>
  97. &nbsp;&nbsp;&nbsp;&nbsp;} else {<br>
  98. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$url = $attributes['default'];<br>
  99. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$text = $content;<br>
  100. &nbsp;&nbsp;&nbsp;&nbsp;}<br>
  101. &nbsp;&nbsp;&nbsp;&nbsp;if ($action == 'validate') {<br>
  102. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (substr ($url, 0, 5) == 'data:' || substr ($url, 0, 5) == 'file:'<br>
  103. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|| substr ($url, 0, 11) == 'javascript:' || substr ($url, 0, 4) == 'jar:') {<br>
  104. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br>
  105. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
  106. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br>
  107. &nbsp;&nbsp;&nbsp;&nbsp;}<br>
  108. &nbsp;&nbsp;&nbsp;&nbsp;return '&lt;a href="'.htmlspecialchars ($url).'"&gt;'.$text.'&lt;/a&gt;';<br>
  109. }<br>
  110. <br>
  111. // Function to include images<br>
  112. function do_bbcode_img ($action, $attributes, $content, $params, $node_object) {<br>
  113. &nbsp;&nbsp;&nbsp;&nbsp;if ($action == 'validate') {<br>
  114. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (substr ($content, 0, 5) == 'data:' || substr ($content, 0, 5) == 'file:'<br>
  115. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|| substr ($content, 0, 11) == 'javascript:' || substr ($content, 0, 4) == 'jar:') {<br>
  116. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br>
  117. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
  118. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br>
  119. &nbsp;&nbsp;&nbsp;&nbsp;}<br>
  120. &nbsp;&nbsp;&nbsp;&nbsp;return '&lt;img src="'.htmlspecialchars($content).'" alt=""&gt;';<br>
  121. }<br>
  122. <br>
  123. $bbcode = new StringParser_BBCode ();<br>
  124. $bbcode->addFilter (STRINGPARSER_FILTER_PRE, 'convertlinebreaks');<br>
  125. <br>
  126. $bbcode->addParser (array ('block', 'inline', 'link', 'listitem'), 'htmlspecialchars');<br>
  127. $bbcode->addParser (array ('block', 'inline', 'link', 'listitem'), 'nl2br');<br>
  128. $bbcode->addParser ('list', 'bbcode_stripcontents');<br>
  129. <br>
  130. $bbcode->addCode ('b', 'simple_replace', null, array ('start_tag' => '&lt;b&gt;', 'end_tag' => '&lt;/b&gt;'),<br>
  131. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'inline', array ('listitem', 'block', 'inline', 'link'), array ());<br>
  132. $bbcode->addCode ('i', 'simple_replace', null, array ('start_tag' => '&lt;i&gt;', 'end_tag' => '&lt;/i&gt;'),<br>
  133. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'inline', array ('listitem', 'block', 'inline', 'link'), array ());<br>
  134. $bbcode->addCode ('url', 'usecontent?', 'do_bbcode_url', array ('usecontent_param' => 'default'),<br>
  135. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'link', array ('listitem', 'block', 'inline'), array ('link'));<br>
  136. $bbcode->addCode ('link', 'callback_replace_single', 'do_bbcode_url', array (),<br>
  137. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'link', array ('listitem', 'block', 'inline'), array ('link'));<br>
  138. $bbcode->addCode ('img', 'usecontent', 'do_bbcode_img', array (),<br>
  139. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'image', array ('listitem', 'block', 'inline', 'link'), array ());<br>
  140. $bbcode->addCode ('bild', 'usecontent', 'do_bbcode_img', array (),<br>
  141. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'image', array ('listitem', 'block', 'inline', 'link'), array ());<br>
  142. $bbcode->setOccurrenceType ('img', 'image');<br>
  143. $bbcode->setOccurrenceType ('bild', 'image');<br>
  144. $bbcode->setMaxOccurrences ('image', 2);<br>
  145. $bbcode->addCode ('list', 'simple_replace', null, array ('start_tag' => '&lt;ul&gt;', 'end_tag' => '&lt;/ul&gt;'),<br>
  146. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'list', array ('block', 'listitem'), array ());<br>
  147. $bbcode->addCode ('*', 'simple_replace', null, array ('start_tag' => '&lt;li&gt;', 'end_tag' => '&lt;/li&gt;'),<br>
  148. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'listitem', array ('list'), array ());<br>
  149. $bbcode->setCodeFlag ('*', 'closetag', BBCODE_CLOSETAG_OPTIONAL);<br>
  150. $bbcode->setCodeFlag ('*', 'paragraphs', true);<br>
  151. $bbcode->setCodeFlag ('list', 'paragraph_type', BBCODE_PARAGRAPH_BLOCK_ELEMENT);<br>
  152. $bbcode->setCodeFlag ('list', 'opentag.before.newline', BBCODE_NEWLINE_DROP);<br>
  153. $bbcode->setCodeFlag ('list', 'closetag.before.newline', BBCODE_NEWLINE_DROP);<br>
  154. $bbcode->setRootParagraphHandling (true);<br>
  155. <br>
  156. $text = '[b]This is a test![/b]';<br>
  157. echo $bbcode->parse ($text);</code></p>
  158. <hr>
  159. <ul>
  160. <li>Next: <a href="chapter10.html">10. Miscellaneous</a></li>
  161. <li>Previous: <a href="chapter8.html">8. Paragraph handling</a></li>
  162. </ul>
  163. </div>
  164. <p id="footer">This is the documentation for the <code>StringParser_BBCode</code> class version <em>0.3.3</em><br>
  165. Author: Christian Seiler, <a href="mailto:webmaster@christian-seiler.de">webmaster@christian-seiler.de</a></p>
  166. </div>
  167. </body>
  168. </html>