Return to Snippet

Revision: 29785
at August 5, 2010 22:45 by Sverri


Updated Code
/*
Takes a string and tries to format it into HTML, using start-of-line rules:
- If the line starts with "+" it is transformed into <h2>
- If the line starts with "-" it is transformed into <ul>...</ul>
- If the line starts with "N." it is transformed into <ol>...</ol>
- If the line starts with anything else it is transformed into <p>
*/
class InputToHTML {
  /*
  A few variables. Set to private because there is no reason to see or access
  them. Simply change "private" to "public" if you are debugging... */
  private $indent, $lines=array(), $final=array(), $ul=array(), $ol=array();
  /*
  Processes text, indent optional */
  public function process($text, $indent=0) {
    /*
    If data is bad then abort */
    if (!is_string($text) or !trim($text)):
      $this->final = false;
      return false;
    endif;
    /*
    Collect initial data */
    $this->lines  = explode("\n", $text);
    $this->indent = str_repeat(' ', $indent);
    /*
    Go over each line separately */
    foreach ($this->lines as $key => &$line):
      /*
      Trim the line */
      $line = trim($line);
      /*
      If the value is empty continue*/
      if (!$line) continue;
      /*
      If the first letter is + then it is a headline */
      if (substr($line,0,1)=='+'):
        $this->lists();
        $line = substr($line, 1);
        $this->final[] = $this->indent . "<h2>$line</h2>";
      /*
      If the first letter is - then it is an unordered list item */
      elseif (substr($line,0,1)=='-'):
        $line = trim(substr($line, 1));
        $this->ul[] = "<li>$line</li>";
      /*
      If the first letter is xx. then it is an ordered list item */
      elseif (preg_match("/[0-9]{1,2}\. .*/", $line)):
        $line = trim(substr($line, strpos($line,'.')+1));
        $this->ol[] = "<li>$line</li>";
      /*
      Otherwise treat the value as a paragraph */
      else:
        $this->lists();
        $this->final[] = $this->indent . "<p>$line</p>";
      endif;
    endforeach;
  }
  /*
  Maintanance of lists is partially deferred to this function for clarity.
  This function is called whenever a line is NOT identified as a list. If
  the list variable is set, then it means that the list has been discontinued.
  It rounds up the list items, adds them to the "final" variable, and resets
  the original list variable */
  private function lists() {
    /*
    Grab a few things */
    $ul =& $this->ul;
    $ol =& $this->ol;
    /*
    Abort if there is nothing to do... */
    if (empty($ul) && empty($ol)) return false;
    /*
    Grab a few more things */
    $i  =& $this->indent;
    $i2 =  (!$i) ? '  ' : str_repeat($i,2);
    /*
    Add contents to "final", and prep ul for reuse */
    if ( ! empty($ul)):
      $this->final[] = $i . "<ul>\n$i2" . implode("\n$i2", $ul) . "\n$i</ul>";
      $ul = array();
    endif;
    /*
    Add contents to "final", and prep ol for reuse */
    if ( ! empty($ol)):
      $this->final[] = $i . "<ol>\n$i2" . implode("\n$i2", $ol) . "\n$i</ol>";
      $ol = array();
    endif;
  }
  /*
  Assembles the HTML simply by imploding the final array */
  public function out() {
    if ($this->final) return implode("\n",$this->final);
    else return '';
  }
  /*
  Over and out */
}

// EXAMPLE

$obj = new InputToHtml();

$obj->process('+Healine 1
This is a paragraph
Another paragraph
- Unordered list item 1
- Unordered list item 2
This is a paragraph
+ Headline 2
1. Ordered list item 1
2. Ordered list item 2
Paragraph...');

echo $obj->out();

/* Output:
<h2>Healine 1</h2>
<p>This is a paragraph</p>
<p>Another paragraph</p>
<ul>
  <li>Unordered list item 1</li>
  <li>Unordered list item 2</li>
</ul>
<p>This is a paragraph</p>
<h2> Headline 2</h2>
<ol>
  <li>Ordered list item 1</li>
  <li>Ordered list item 2</li>
</ol>
<p>Paragraph...</p> */

Revision: 29784
at August 5, 2010 22:43 by Sverri


Updated Code
/*
Takes a string and tries to format it into HTML, using start-of-line rules:
- If the line starts with "+" it is transformed into <h2>
- If the line starts with "-" it is transformed into <ul>...</ul>
- If the line starts with "N." it is transformed into <ol>...</ol>
- If the line starts with anything else it is transformed into <p>
*/
class InputToHTML {
  /*
  A few variables. Set to private because there is no reason to see or access
  them. Simply change "private" to "public" if you are debugging... */
  private $indent, $lines=array(), $final=array(), $ul=array(), $ol=array();
  /*
  Processes text, indent optional */
  public function process($text, $indent=0) {
    /*
    If data is bad then abort */
    if (!is_string($text) or !trim($text)):
      $this->final = false;
      return false;
    endif;
    /*
    Collect initial data */
    $this->lines  = explode("\n", $text);
    $this->indent = str_repeat(' ', $indent);
    /*
    Go over each line separately */
    foreach ($this->lines as $key => &$line):
      /*
      Trim the line */
      $line = trim($line);
      /*
      If the value is empty continue*/
      if (!$line) continue;
      /*
      If the first letter is + then it is a headline */
      if (substr($line,0,1)=='+'):
        $this->lists();
        $line = substr($line, 1);
        $this->final[] = $this->indent . "<h2>$line</h2>";
      /*
      If the first letter is - then it is an unordered list item */
      elseif (substr($line,0,1)=='-'):
        $line = trim(substr($line, 1));
        $this->ul[] = "<li>$line</li>";
      /*
      If the first letter is xx. then it is an ordered list item */
      elseif (preg_match("/[0-9]{1,2}\. .*/", $line)):
        $line = trim(substr($line, strpos($line,'.')+1));
        $this->ol[] = "<li>$line</li>";
      /*
      Otherwise treat the value as a paragraph */
      else:
        $this->lists();
        $this->final[] = $this->indent . "<p>$line</p>";
      endif;
    endforeach;
  }
  /*
  Maintanance of lists is partially deferred to this function for clarity.
  This function is called whenever a line is NOT identified as a list. If
  the list variable is set, then it means that the list has been discontinued.
  It rounds up the list items, adds them to the "final" variable, and resets
  the original list variable */
  private function lists() {
    /*
    Grab a few things */
    $ul =& $this->ul;
    $ol =& $this->ol;
    /*
    Abort if there is nothing to do... */
    if (empty($ul) && empty($ol)) return false;
    /*
    Grab a few more things */
    $i  =& $this->indent;
    $i2 =  (!$i)?'  ':str_repeat($this->indent, 2);
    /*
    Add contents to "final", and prep ul for reuse */
    if ( ! empty($ul)):
      $this->final[] = $i . "<ul>\n$i2" . implode("\n$i2", $ul) . "\n$i</ul>";
      $ul = array();
    endif;
    /*
    Add contents to "final", and prep ol for reuse */
    if ( ! empty($ol)):
      $this->final[] = $i . "<ol>\n$i2" . implode("\n$i2", $ol) . "\n$i</ol>";
      $ol = array();
    endif;
  }
  /*
  Assembles the HTML simply by imploding the final array */
  public function out() {
    if ($this->final) return implode("\n",$this->final);
    else return '';
  }
  /*
  Over and out */
}

// EXAMPLE

$obj = new InputToHtml();

$obj->process('+Healine 1
This is a paragraph
Another paragraph
- Unordered list item 1
- Unordered list item 2
This is a paragraph
+ Headline 2
1. Ordered list item 1
2. Ordered list item 2
Paragraph...');

echo $obj->out();

/* Output:
<h2>Healine 1</h2>
<p>This is a paragraph</p>
<p>Another paragraph</p>
<ul>
  <li>Unordered list item 1</li>
  <li>Unordered list item 2</li>
</ul>
<p>This is a paragraph</p>
<h2> Headline 2</h2>
<ol>
  <li>Ordered list item 1</li>
  <li>Ordered list item 2</li>
</ol>
<p>Paragraph...</p> */

Revision: 29783
at August 4, 2010 19:16 by Sverri


Updated Code
/*
Takes a string and tries to format it into HTML, using start-of-line rules:
- If the line starts with "+" it is transformed into <h2>
- If the line starts with "-" it is transformed into <ul>...</ul>
- If the line starts with "N." it is transformed into <ol>...</ol>
- If the line starts with anything else it is transformed into <p>
*/
class InputToHTML {
  /*
  A few variables. Set to private because there is no reason to see or access
  them. Simply change "private" to "public" if you are debugging... */
  private $indent, $lines=array(), $final=array(), $ul=array(), $ol=array();
  /*
  Processes text, indent optional */
  public function process($text, $indent=0) {
    /*
    If data is bad then abort */
    if (!is_string($text) or !trim($text)):
      $this->final = false;
      return false;
    endif;
    /*
    Collect initial data */
    $this->lines  = explode("\n", $text);
    $this->indent = str_repeat(' ', $indent);
    /*
    Go over each line separately */
    foreach ($this->lines as $key => &$line):
      /*
      Trim the line */
      $line = trim($line);
      /*
      If the value is empty continue*/
      if (!$line) continue;
      /*
      If the first letter is + then it is a headline */
      if (substr($line,0,1)=='+'):
        $this->lists();
        $line = substr($line, 1);
        $this->final[] = $this->indent . "<h2>$line</h2>";
      /*
      If the first letter is - then it is an unordered list item */
      elseif (substr($line,0,1)=='-'):
        $line = trim(substr($line, 1));
        $this->ul[] = "<li>$line</li>";
      /*
      If the first letter is xx. then it is an ordered list item */
      elseif (preg_match("/[0-9]{1,2}\. .*/", $line)):
        $line = trim(substr($line, strpos($line,'.')+1));
        $this->ol[] = "<li>$line</li>";
      /*
      Otherwise treat the value as a paragraph */
      else:
        $this->lists();
        $this->final[] = $this->indent . "<p>$line</p>";
      endif;
    endforeach;
  }
  /*
  Maintanance of lists is partially deferred to this function for clarity.
  This function is called whenever a line is NOT identified as a list. If
  the list variable is set, then it means that the list has been discontinued.
  It rounds up the list items, adds them to the "final" variable, and resets
  the original list variable */
  private function lists() {
    /*
    Grab a few things */
    $list1 =& $this->ul;
    $list2 =& $this->ol;
    /*
    Abort if there is nothing to do... */
    if (empty($list1) && empty($list2)) return false;
    /*
    Grab a few more things */
    $i  =& $this->indent;
    $i2 =  (!$i)?'  ':str_repeat($this->indent, 2);
    /*
    Add contents to "final", and prep for reuse */
    if ( ! empty($list1)):
      $this->final[] = $i . "<ul>\n$i2" . implode("\n$i2", $list1) . "\n$i</ul>";
      $list1 = array();
    endif;
    /*
    Add contents to "final", and prep for reuse */
    if ( ! empty($list2)):
      $this->final[] = $i . "<ol>\n$i2" . implode("\n$i2", $list2) . "\n$i</ol>";
      $list2 = array();
    endif;
  }
  /*
  When the object is echoed out turn it into a string. Hoorah for being lazy */
  public function out() {
    if ($this->final) return implode("\n",$this->final);
    else return '';
  }
  /*
  Over and out */
}

// EXAMPLE

$obj = new InputToHtml();

$obj->process('+Healine 1
This is a paragraph
Another paragraph
- Unordered list item 1
- Unordered list item 2
This is a paragraph
+ Headline 2
1. Ordered list item 1
2. Ordered list item 2
Paragraph...');

echo $obj->out();

/* Output:
<h2>Healine 1</h2>
<p>This is a paragraph</p>
<p>Another paragraph</p>
<ul>
  <li>Unordered list item 1</li>
  <li>Unordered list item 2</li>
</ul>
<p>This is a paragraph</p>
<h2> Headline 2</h2>
<ol>
  <li>Ordered list item 1</li>
  <li>Ordered list item 2</li>
</ol>
<p>Paragraph...</p> */

Revision: 29782
at August 4, 2010 19:15 by Sverri


Initial Code
/*
Takes a string and tries to format it into HTML, using start-of-line rules:
- If the line starts with "+" it is transformed into <h2>
- If the line starts with "-" it is transformed into <ul>...</ul>
- If the line starts with "N." it is transformed into <ol>...</ol>
- If the line starts with anything else it is transformed into <p>
*/
class InputToHTML {
  /*
  A few variables. Set to private because there is no reason to see or access
  them. Simply change "private" to "public" if you are debugging... */
  private $indent, $lines=array(), $final=array(), $ul=array(), $ol=array();
  /*
  Processes text, indent optional */
  public function process($text, $indent=0) {
    /*
    If data is bad then abort */
    if (!is_string($text) or !trim($text)):
      $this->final = false;
      return false;
    endif;
    /*
    Collect initial data */
    $this->lines  = explode("\n", $text);
    $this->indent = str_repeat(' ', $indent);
    /*
    Go over each line separately */
    foreach ($this->lines as $key => &$line):
      /*
      Trim the line */
      $line = trim($line);
      /*
      If the value is empty continue*/
      if (!$line) continue;
      /*
      If the first letter is + then it is a headline */
      if (substr($line,0,1)=='+'):
        $this->lists();
        $line = substr($line, 1);
        $this->final[] = $this->indent . "<h2>$line</h2>";
      /*
      If the first letter is - then it is an unordered list item */
      elseif (substr($line,0,1)=='-'):
        $line = trim(substr($line, 1));
        $this->ul[] = "<li>$line</li>";
      /*
      If the first letter is xx. then it is an ordered list item */
      elseif (preg_match("/[0-9]{1,2}\. .*/", $line)):
        $line = trim(substr($line, strpos($line,'.')+1));
        $this->ol[] = "<li>$line</li>";
      /*
      Otherwise treat the value as a paragraph */
      else:
        $this->lists();
        $this->final[] = $this->indent . "<p>$line</p>";
      endif;
    endforeach;
  }
  /*
  Maintanance of lists is partially deferred to this function for clarity.
  This function is called whenever a line is NOT identified as a list. If
  the list variable is set, then it means that the list has been discontinued.
  It rounds up the list items, adds them to the "final" variable, and resets
  the original list variable */
  private function lists() {
    /*
    Grab a few things */
    $list1 =& $this->ul;
    $list2 =& $this->ol;
    /*
    Abort if there is nothing to do... */
    if (empty($list1) && empty($list2)) return false;
    /*
    Grab a few more things */
    $i  =& $this->indent;
    $i2 =  (!$i)?'  ':str_repeat($this->indent, 2);
    /*
    Add contents to "final", and prep for reuse */
    if ( ! empty($list1)):
      $this->final[] = $i . "<ul>\n$i2" . implode("\n$i2", $list1) . "\n$i</ul>";
      $list1 = array();
    endif;
    /*
    Add contents to "final", and prep for reuse */
    if ( ! empty($list2)):
      $this->final[] = $i . "<ol>\n$i2" . implode("\n$i2", $list2) . "\n$i</ol>";
      $list2 = array();
    endif;
  }
  /*
  When the object is echoed out turn it into a string. Hoorah for being lazy */
  public function out() {
    if ($this->final) return implode("\n",$this->final);
    else return '';
  }
  /*
  Over and out */
}

// EXAMPLE

$obj = new InputToHtml();

$obj->process('+Healine 1
This is a paragraph
Another paragraph
- Unordered list item 1
- Unordered list item 2
This is a paragraph
+ Headline 2
1. Ordered list item 1
2. Ordered list item 2
Paragraph...');

echo $obj->out();

/* Output:
&lth2&gtHealine 1&lt/h2&gt
&ltp&gtThis is a paragraph&lt/p&gt
&ltp&gtAnother paragraph&lt/p&gt
&ltul&gt
  &ltli&gtUnordered list item 1&lt/li&gt
  &ltli&gtUnordered list item 2&lt/li&gt
&lt/ul&gt
&ltp&gtThis is a paragraph&lt/p&gt
&lth2&gt Headline 2&lt/h2&gt
&ltol&gt
  &ltli&gtOrdered list item 1&lt/li&gt
  &ltli&gtOrdered list item 2&lt/li&gt
&lt/ol&gt
&ltp&gtParagraph...&lt/p&gt */

Initial URL


Initial Description
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).

Initial Title
Turn input string into formatted HTML

Initial Tags


Initial Language
PHP