Turn input string into formatted HTML


/ Published in: PHP
Save to your folder(s)

Takes a string (from textarea, et alii) and turns it into formatted HTML.\\\\r\\\\n\\\\r\\\\nLines of text are turned into paragraphs, unless they start with \\\\\\\"+\\\\\\\" (headline); \\\\\\\"-\\\\\\\" (unordered list); or \\\\\\\"n.\\\\\\\" (ordered list).


Copy this code and paste it in your HTML
  1. /*
  2. Takes a string and tries to format it into HTML, using start-of-line rules:
  3. - If the line starts with "+" it is transformed into <h2>
  4. - If the line starts with "-" it is transformed into <ul>...</ul>
  5. - If the line starts with "N." it is transformed into <ol>...</ol>
  6. - If the line starts with anything else it is transformed into <p>
  7. */
  8. class InputToHTML {
  9. /*
  10.   A few variables. Set to private because there is no reason to see or access
  11.   them. Simply change "private" to "public" if you are debugging... */
  12. private $indent, $lines=array(), $final=array(), $ul=array(), $ol=array();
  13. /*
  14.   Processes text, indent optional */
  15. public function process($text, $indent=0) {
  16. /*
  17.   If data is bad then abort */
  18. if (!is_string($text) or !trim($text)):
  19. $this->final = false;
  20. return false;
  21. endif;
  22. /*
  23.   Collect initial data */
  24. $this->lines = explode("\n", $text);
  25. $this->indent = str_repeat(' ', $indent);
  26. /*
  27.   Go over each line separately */
  28. foreach ($this->lines as $key => &$line):
  29. /*
  30.   Trim the line */
  31. $line = trim($line);
  32. /*
  33.   If the value is empty continue*/
  34. if (!$line) continue;
  35. /*
  36.   If the first letter is + then it is a headline */
  37. if (substr($line,0,1)=='+'):
  38. $this->lists();
  39. $line = substr($line, 1);
  40. $this->final[] = $this->indent . "<h2>$line</h2>";
  41. /*
  42.   If the first letter is - then it is an unordered list item */
  43. elseif (substr($line,0,1)=='-'):
  44. $line = trim(substr($line, 1));
  45. $this->ul[] = "<li>$line</li>";
  46. /*
  47.   If the first letter is xx. then it is an ordered list item */
  48. elseif (preg_match("/[0-9]{1,2}\. .*/", $line)):
  49. $line = trim(substr($line, strpos($line,'.')+1));
  50. $this->ol[] = "<li>$line</li>";
  51. /*
  52.   Otherwise treat the value as a paragraph */
  53. else:
  54. $this->lists();
  55. $this->final[] = $this->indent . "<p>$line</p>";
  56. endif;
  57. endforeach;
  58. }
  59. /*
  60.   Maintanance of lists is partially deferred to this function for clarity.
  61.   This function is called whenever a line is NOT identified as a list. If
  62.   the list variable is set, then it means that the list has been discontinued.
  63.   It rounds up the list items, adds them to the "final" variable, and resets
  64.   the original list variable */
  65. private function lists() {
  66. /*
  67.   Grab a few things */
  68. $ul =& $this->ul;
  69. $ol =& $this->ol;
  70. /*
  71.   Abort if there is nothing to do... */
  72. if (empty($ul) && empty($ol)) return false;
  73. /*
  74.   Grab a few more things */
  75. $i =& $this->indent;
  76. $i2 = (!$i) ? ' ' : str_repeat($i,2);
  77. /*
  78.   Add contents to "final", and prep ul for reuse */
  79. if ( ! empty($ul)):
  80. $this->final[] = $i . "<ul>\n$i2" . implode("\n$i2", $ul) . "\n$i</ul>";
  81. $ul = array();
  82. endif;
  83. /*
  84.   Add contents to "final", and prep ol for reuse */
  85. if ( ! empty($ol)):
  86. $this->final[] = $i . "<ol>\n$i2" . implode("\n$i2", $ol) . "\n$i</ol>";
  87. $ol = array();
  88. endif;
  89. }
  90. /*
  91.   Assembles the HTML simply by imploding the final array */
  92. public function out() {
  93. if ($this->final) return implode("\n",$this->final);
  94. else return '';
  95. }
  96. /*
  97.   Over and out */
  98. }
  99.  
  100. // EXAMPLE
  101.  
  102. $obj = new InputToHtml();
  103.  
  104. $obj->process('+Healine 1
  105. This is a paragraph
  106. Another paragraph
  107. - Unordered list item 1
  108. - Unordered list item 2
  109. This is a paragraph
  110. + Headline 2
  111. 1. Ordered list item 1
  112. 2. Ordered list item 2
  113. Paragraph...');
  114.  
  115. echo $obj->out();
  116.  
  117. /* Output:
  118. <h2>Healine 1</h2>
  119. <p>This is a paragraph</p>
  120. <p>Another paragraph</p>
  121. <ul>
  122.   <li>Unordered list item 1</li>
  123.   <li>Unordered list item 2</li>
  124. </ul>
  125. <p>This is a paragraph</p>
  126. <h2> Headline 2</h2>
  127. <ol>
  128.   <li>Ordered list item 1</li>
  129.   <li>Ordered list item 2</li>
  130. </ol>
  131. <p>Paragraph...</p> */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.