core
[ class tree: core ] [ index: core ] [ all elements ]

Source for file PlainTextFormatter.class.php

Documentation is available at PlainTextFormatter.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package core
  9.  */
  10. if!defined"CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
  14.   exit();
  15. }
  16.  
  17. /**
  18.  *
  19.  */
  20. include_onceCLASSPATH."core/Strings.class.php" );
  21.  
  22. /**
  23.  *
  24.  */
  25. include_onceCLASSPATH."RegExpConstants.inc.php" );
  26.  
  27. /**
  28.  * @static
  29.  * @package   core
  30.  * @version   0.2.32
  31.  *
  32.  * @author    Daniel Plücken <daniel@debakel.net>
  33.  *
  34.  * @license   http://www.gnu.org/copyleft/lesser.html
  35.  *             GNU Lesser General Public License
  36.  * @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
  37.  *
  38.  *  This library is free software; you can redistribute it and/or
  39.  *  modify it under the terms of the GNU Lesser General Public
  40.  *  License as published by the Free Software Foundation; either
  41.  *  version 2.1 of the License.
  42.  *
  43.  *  This library is distributed in the hope that it will be useful,
  44.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  45.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46.  *  GNU Lesser General Public License for more details.
  47.  *
  48.  *  You should have received a copy of the GNU Lesser General
  49.  *  Public License along with this library; if not, write to the
  50.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  51.  *  Boston, MA 02111-1307 USA
  52.  */
  53. {
  54.    /**
  55.     * @var    integer 
  56.     * @access public
  57.     */
  58.    var $indent   =  5;
  59.  
  60.    /**
  61.     * @var    integer 
  62.     * @access public
  63.     */
  64.    var $maxChars = 70;
  65.  
  66.  
  67.  
  68.  
  69.    /**
  70.     * Breaks a line after $maxChars characters if no word will be cut else
  71.     * earlier and indent the line for $indent chars
  72.     *
  73.     * @version 1.0
  74.     * @since   0.1.0
  75.     * @author  Daniel Plücken <daniel@debakel.net>
  76.     * @access  public
  77.     * @static
  78.     * @param   string  $text      The string which should be modified.
  79.     * @param   integer $max_chars The maximum of characters that should be in a
  80.                                   line.
  81.     * @param   integer $indent    The indent of all lines.
  82.     * @return  string 
  83.     */
  84.    function breakBy$text$max_chars 0$indent )
  85.    {
  86.       $param func_get_args();
  87.  
  88.       $max_chars $max_chars 10
  89.                    ? $max_chars
  90.                    : $this->maxChars;
  91.  
  92.       $text Strings::reduceWhiteSpace$text );
  93.  
  94.       if$max_chars >= strlen$text ) )
  95.          $out $text;
  96.       else
  97.       {
  98.          $rowChars = isset$indent )
  99.                      ? absintval$indent ) ) 1
  100.                      : -1;
  101.  
  102.          $switchChar "";
  103.  
  104.          $out $indent str_repeat" "$indent "";
  105.  
  106.          $dobr true;
  107.          while$pos strpos$text" " ) )
  108.          {
  109.              $tmplen $switchChar == " "
  110.                        ? 0;
  111.              $rowChars += $pos $tmplen;
  112.  
  113.              $posN strpos$text"\n" );
  114.  
  115.              $text_piece substr$text0$pos );
  116.  
  117.              if(
  118.                  $rowChars >= $max_chars
  119.              && $posN $pos
  120.                 || $posN === false )
  121.              && $dobr
  122.                )
  123.                $switchChar " \n";
  124.              else
  125.                $switchChar " ";
  126.  
  127.              $out .= $switchChar.$text_piece;
  128.  
  129.              if(
  130.                  $posN $pos
  131.               && $posN !== false
  132.                )
  133.                $rowChars $pos $posN );
  134.  
  135.              if$switchChar == " \n" )
  136.                $rowChars $pos;
  137.  
  138.              $text substr$text$pos );
  139.          }
  140.          $out .= $switchChar.$text;
  141.       }
  142.  
  143.       return trim$out );
  144.    }
  145.  
  146.  
  147.  
  148.    /**
  149.     * Adds $indent spaces in front of each line.
  150.     *
  151.     * @version 1.0
  152.     * @since   0.1.1
  153.     * @author  Daniel Plücken <daniel@debakel.net>
  154.     * @access  public
  155.     * @static
  156.     * @param   string  $text      The string which should be modified.
  157.     * @param   integer $maxChars  The maximum of characters that should be in a
  158.                                   line.
  159.     * @param   integer $indent    The indent of all lines.
  160.     * @return  string 
  161.     */
  162.    function indentText$text ""$indent )
  163.    {
  164.       ifempty$indent ) )
  165.         $indent $this->indent;
  166.  
  167.       $str_indent str_repeat" "$indent );
  168.  
  169.       $doindent true;
  170.       $out      "";
  171.       $tok_mem  "   ";
  172.       $tok      strtok$text"\n" );
  173.  
  174.       while$tok )
  175.       {
  176.           // except textareas
  177.           ifpreg_match"!.*</textarea>.*!i"$tok_mem ) )
  178.             $doindent true;
  179.  
  180.           $doindent $doindent
  181.                    && !preg_match"!.*<textarea.*!i"$tok_mem );
  182.           // except textareas */
  183.  
  184.           if$doindent )
  185.             $out .= $str_indent.$tok."\n";
  186.           else
  187.             $out .= $tok."\n";
  188.  
  189.           $tok_mem $tok;
  190.           $tok strtok"\n" );
  191.       }
  192.  
  193.       return $out;
  194.    }
  195.  
  196.  
  197.  
  198.    /**
  199.     * Adds $indent spaces in front of each line from given string and breaks
  200.     * after characters count stored in the class variable $maxChars, but only
  201.     * if no word will be break (then the break will be earlier; before the
  202.     * word).
  203.     *
  204.     * @version 1.0
  205.     * @since   0.1.1
  206.     * @author  Daniel Plücken <daniel@debakel.net>
  207.     * @access  public
  208.     * @static
  209.     * @param   string  $text             The string which should be modified.
  210.     * @param   integer $indent           The indent of all lines.
  211.     * @param   integer $max_chars        The maximum of characters that should
  212.     *                                     be in a line.
  213.     * @param   integer $first_line_begin The negative indent of the first line.
  214.     * @return  string 
  215.     */
  216.    function format(
  217.                     $text,
  218.                     $indent 0,
  219.                     $max_chars 70,
  220.                     $first_line_begin 0
  221.                   )
  222.    {
  223.       $text Strings::reduceWhiteSpace$text );
  224.  
  225.       if(
  226.           !empty$first_line_begin )
  227.        && $first_line_begin 0
  228.         )
  229.         $firstlineBegin $first_line_begin;
  230.       else
  231.         $firstlineBegin 0;
  232.  
  233.       $indent    !empty$indent )
  234.                    ? $indent
  235.                    : $this->indent;
  236.  
  237.       $maxChars  !empty$max_chars )
  238.                    ? $max_chars
  239.                    : $this->maxChars;
  240.  
  241.       $showChars $maxChars $indent;
  242.  
  243.       $out PlainTextFormatter
  244.            ::indentText(
  245.                            PlainTextFormatter
  246.                            ::breakBy(
  247.                                       $text,
  248.                                       $showChars,
  249.                                       $firstlineBegin
  250.                                     ),
  251.                            $indent
  252.                        );
  253.       if(
  254.           !empty$first_line_begin )
  255.        && $first_line_begin 0
  256.         )
  257.         $out substr$out$first_line_begin * -) );
  258.  
  259.       return $out;
  260.    }
  261.  
  262.  
  263.  
  264.    /**
  265.     * Cuts the given text that the length is not longer than $max_chars
  266.     * characters. The last three characters will be "...".
  267.     *
  268.     * @version 1.1
  269.     * @since   0.1.1
  270.     * @author  Daniel Plücken <daniel@debakel.net>
  271.     * @access  public
  272.     * @static
  273.     * @param   string  $text  The string which should be modified.
  274.     * @return  string 
  275.     */
  276.    function maxchars$text$max_chars )
  277.    {
  278.       $inputstr  empty$text "" $text;
  279.       $maxChars  empty$max_chars 15 $max_chars;
  280.       $out       strlen$inputstr $maxChars )
  281.                    ? substr$inputstr0$maxChars )."..."
  282.                    : $inputstr;
  283.  
  284.       return $out;
  285.    }
  286.  
  287.  
  288.  
  289.    /**
  290.     * Replaces special characters to entities. This is very useful for embed
  291.     * HTML in XML.
  292.     *
  293.     * @version 1.0
  294.     * @since   0.1.6
  295.     * @author  Daniel Plücken <daniel@debakel.net>
  296.     * @access  public
  297.     * @static
  298.     * @param   string  $text  The string which should be modified.
  299.     * @return  string 
  300.     */
  301.    function makeXMLEmbedded$text )
  302.    {
  303.       $outputString str_replace"\"""&quot;"$text );
  304.       $outputString str_replace"'""&prime;"$outputString );
  305.       $outputString str_replace"<""&lt;"$outputString );
  306.       $outputString str_replace">""&gt;"$outputString );
  307.  
  308.       return $outputString;
  309.    }
  310.  
  311.  
  312.  
  313.    /**
  314.     * Replaces entities to special characters. This is very useful for handle
  315.     * HTML embedded in XML.
  316.     *
  317.     * @version 1.0
  318.     * @since   0.1.6
  319.     * @author  Daniel Plücken <daniel@debakel.net>
  320.     * @access  public
  321.     * @static
  322.     * @param   string  $text  The string which should be modified.
  323.     * @return  string 
  324.     */
  325.    function extractXMLEmbedded$text )
  326.    {
  327.       $outputString str_replace"&quot;""\""$text );
  328.       $outputString str_replace"&prime;""'"$outputString );
  329.       $outputString str_replace"&lt;",    "<"$outputString );
  330.       $outputString str_replace"&gt;",    ">"$outputString );
  331.  
  332.       return $outputString;
  333.    }
  334.  
  335.  
  336.  
  337.    /**
  338.     * Formats sql querys.
  339.     *
  340.     * @version 1.11
  341.     * @since   0.2.2
  342.     * @author  Daniel Plücken <daniel@debakel.net>
  343.     * @access  public
  344.     * @static
  345.     * @param   string  $str_in The string which should be modified.
  346.     * @return  string 
  347.     */
  348.    function formatSQL$str_in )
  349.    {
  350.      $out preg_replace(
  351.                           "!^\s*(SELECT|INSERT|UPDATE)\s+!is",
  352.                           "        $1 "$str_in
  353.                         );
  354.      $str_fieldname_pattern "(?:`[^`]+?`|[^- .+`;'=\\\\()*]+?)";
  355.      if preg_match"!^        SELECT!i"$out ) )
  356.      {
  357.          $fields_of_select preg_replace(
  358.                           "!^        SELECT (.+?)FROM.+!is",
  359.                           "$1"$out
  360.                                          );
  361.          $fields_of_select preg_replace(
  362.                           "!((?:(?:DISTINCT\s+)?[-_a-z0-9]*\.?".$str_fieldname_pattern."|".SINGLE_QUOTATION_STRING.")(?:\s+AS\s+".$str_fieldname_pattern.")?\s*,)\s*!is",
  363.                           "$1\r\n               ",
  364.                           $fields_of_select
  365.                                          );
  366.          $tables_of_select preg_replace(
  367.                           "!^        SELECT (?:.+?)FROM(.+?)(?:LEFT\s+JOIN|RIGHT\s+JOIN|NATURAL\s+JOIN|WHERE|ORDER\s+BY|GROUP\s+BY).*!is",
  368.                           "$1"$out
  369.                                          );
  370.          $tables_of_select preg_replace(
  371.                           "!((?:[-_a-z0-9]*\.?".$str_fieldname_pattern."|".SINGLE_QUOTATION_STRING.")(?:\s+AS\s+".$str_fieldname_pattern.")?\s*,)\s*!is",
  372.                           "$1\r\n               ",
  373.                           $tables_of_select
  374.                                          );
  375.          $out preg_replace(
  376.                           "!^        SELECT (?:.+?)FROM(?:.+?)(LEFT\s+JOIN|RIGHT\s+JOIN|NATURAL\s+JOIN|WHERE|ORDER\s+BY|GROUP\s+BY)!is",
  377.  
  378.                           "        SELECT ".$fields_of_select."\r\n"
  379.                          ."          FROM ".$tables_of_select."$1"$out
  380.                                          );
  381.      }
  382.      $out preg_replace"!^\s*REPLACE +!is",        "       REPLACE "$out );
  383.      $out preg_replace(
  384.                            "#\s+(?<!LEFT|RIGHT|NATURAL)\s+JOIN +#is",
  385.                            " \r\n          JOIN "$out
  386.                         );
  387.      $out preg_replace"!\s+LEFT\s+JOIN +!is",  " \r\n     LEFT JOIN "$out );
  388.      $out preg_replace"!\s+RIGHT\s+JOIN +!is"" \r\n    RIGHT JOIN "$out );
  389.      $out preg_replace"!\s+NATURAL\s+JOIN +!is"" \r\nNATURAL JOIN "$out );
  390.      $out preg_replace"!\s+FROM +!is",       " \r\n          FROM "$out );
  391.      $out preg_replace"!\s+(OR|ON) +!is",    " \r\n            $1 "$out );
  392.      $out preg_replace"!\s+AND +!is",        " \r\n           AND "$out );
  393.      $out preg_replace"!\s+WHERE +!is",      " \r\n         WHERE "$out );
  394.  
  395.      $out preg_replace"!\s+AS\s+!is",          " AS "$out );
  396.  
  397.      $out preg_replace"!\s+(ORDER\s+BY|GROUP\s+BY) +!is"" \r\n      $1 "$out );
  398.      return $out;
  399.    }
  400.  
  401.  
  402.  
  403.    /**
  404.     * Replaces character in that kind, that only html-character remain.
  405.     *
  406.     * @version 1.1
  407.     * @since   0.1.3
  408.     * @author  Daniel Plücken <daniel@debakel.net>
  409.     * @access  public
  410.     * @static
  411.     * @param   string  $inputstr  The string which should be modified.
  412.     * @return  string 
  413.     */
  414.    function text2HTML$inputstr )
  415.    {
  416.       $out htmlentities$inputstr );
  417.       $out str_replace"\r\n""<br />"$out );
  418.       $out str_replace"\r",   "<br />"$out );
  419.       $out str_replace"\n",   "<br />"$out );
  420.  
  421.       return $out;
  422.    }
  423.  
  424.  
  425.  
  426.    /**
  427.     * Replaces character in that kind, that only ascii-character remain.
  428.     *
  429.     * @version 1.0
  430.     * @since   0.1.2
  431.     * @author  Daniel Plücken <daniel@debakel.net>
  432.     * @access  public
  433.     * @static
  434.     * @param   string  $inputstr  The string which should be modified.
  435.     * @return  string 
  436.     */
  437.    function Plaintext2ASCII$inputstr )
  438.    {
  439.       $out $inputstr;
  440.       if!empty$out ) )
  441.       {
  442.          $out str_replace"\xE4""ae"$out );
  443.          $out str_replace"\xF6""oe"$out );
  444.          $out str_replace"\xFC""ue"$out );
  445.          $out str_replace"\xC4""Ae"$out );
  446.          $out str_replace"\xD6""Oe"$out );
  447.          $out str_replace"\xDC""Ue"$out );
  448.          $out str_replace"\xDF""ss"$out );
  449.       }
  450.  
  451.       return $out;
  452.    }
  453.  
  454.  
  455.  
  456.    /**
  457.     * Replaces each in series appearing whitespaces inside a textarea to only
  458.     * one whitespace.
  459.     *
  460.     * @version 1.0
  461.     * @since   0.1.3
  462.     * @author  Daniel Plücken <daniel@debakel.net>
  463.     * @access  public
  464.     * @static
  465.     * @param   string  $string  The string which should be modified.
  466.     * @return  string 
  467.     */
  468.    function reduceWhiteSpaceOfTextAreas$string )
  469.    {
  470.       $outputstr $string;
  471.       $outputstr preg_replace(
  472.                                  "!<textarea(.*?)>!i",
  473.                                  "<textarea$1>[-#BEGIN#-]",
  474.                                  $outputstr
  475.                                );
  476.       $outputstr preg_replace(
  477.                                  "!</textarea>!i",
  478.                                  "[-#END#-]</textarea>",
  479.                                  $outputstr
  480.                                );
  481.       $pos_begin 0;
  482.       while$pos_begin strpos$outputstr"[-#BEGIN#-]"$pos_begin ) )
  483.       {
  484.          $pos_end strpos$outputstr"[-#END#-]"$pos_begin );
  485.          $tmp_str substr(
  486.                             $outputstr,
  487.                             $pos_begin 11,
  488.                             $pos_end $pos_begin 11
  489.                           );
  490.          $tmp_rep_str trim$tmp_str );
  491.          $tmp_rep_str preg_replace"! +!",  " "$tmp_rep_str );
  492.          $tmp_rep_str str_replace(  "\r\n""\n"$tmp_rep_str );
  493.          $tmp_rep_str str_replace(  "\r"  "\n"$tmp_rep_str );
  494.          $tmp_rep_str str_replace(  "\n " "\n"$tmp_rep_str );
  495.  
  496.          $outputstr str_replace(
  497.                                    "[-#BEGIN#-]".$tmp_str."[-#END#-]",
  498.                                    $tmp_rep_str,
  499.                                    $outputstr
  500.                                  );
  501.       }
  502.  
  503.       return $outputstr;
  504.    }
  505.  
  506.  
  507.  
  508.    /**
  509.     * Sets the indent of all lines of a string if the function "breakBy",
  510.     * "format" or "blockqoutePlainText" is used and no indent is given by
  511.     * parameter.
  512.     *
  513.     * @version 1.0
  514.     * @since   0.1.0
  515.     * @author  Daniel Plücken <daniel@debakel.net>
  516.     * @access  public
  517.     * @param   integer $int 
  518.     */
  519.    function setIndent$int )
  520.    $this->indent = $int}
  521.  
  522.  
  523.  
  524.    /**
  525.     * Sets the maximum of characters that should be in a line if the function
  526.     * "breakBy", "format" or "blockqoutePlainText" is used and no indent is
  527.     * given by parameter.
  528.     *
  529.     * @version 1.0
  530.     * @since   0.1.0
  531.     * @author  Daniel Plücken <daniel@debakel.net>
  532.     * @access  public
  533.     * @param   integer $int 
  534.     */
  535.    function setMaxChars$int )
  536.    $this->maxChars = $int}
  537. // End of class PlainTextFormatter
  538. ?>

Documentation generated on Thu, 05 Jun 2008 19:14:47 +0200 by phpDocumentor 1.4.1