Source for file PlainTextFormatter.class.php
Documentation is available at PlainTextFormatter.class.php
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
include_once( CLASSPATH. "core/Strings.class.php" );
include_once( CLASSPATH. "RegExpConstants.inc.php" );
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* Breaks a line after $maxChars characters if no word will be cut else
* earlier and indent the line for $indent chars
* @author Daniel Plücken <daniel@debakel.net>
* @param string $text The string which should be modified.
* @param integer $max_chars The maximum of characters that should be in a
* @param integer $indent The indent of all lines.
function breakBy( $text, $max_chars = 0, $indent = 0 )
$max_chars = $max_chars > 10
if( $max_chars >= strlen( $text ) )
$rowChars = isset ( $indent )
$out = $indent > 0 ? str_repeat( " ", $indent ) : "";
while( $pos = strpos( $text, " " ) )
$tmplen = $switchChar == " "
$rowChars += $pos + $tmplen;
$posN = strpos( $text, "\n" );
$text_piece = substr( $text, 0, $pos );
$out .= $switchChar. $text_piece;
$rowChars = ( $pos - $posN - 1 );
if( $switchChar == " \n" )
$text = substr( $text, $pos + 1 );
$out .= $switchChar. $text;
* Adds $indent spaces in front of each line.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $text The string which should be modified.
* @param integer $maxChars The maximum of characters that should be in a
* @param integer $indent The indent of all lines.
if( preg_match( "!.*</textarea>.*!i", $tok_mem ) )
$out .= $str_indent. $tok. "\n";
* Adds $indent spaces in front of each line from given string and breaks
* after characters count stored in the class variable $maxChars, but only
* if no word will be break (then the break will be earlier; before the
* @author Daniel Plücken <daniel@debakel.net>
* @param string $text The string which should be modified.
* @param integer $indent The indent of all lines.
* @param integer $max_chars The maximum of characters that should
* @param integer $first_line_begin The negative indent of the first line.
!empty( $first_line_begin )
$firstlineBegin = $first_line_begin;
$indent = !empty( $indent )
$maxChars = !empty( $max_chars )
$showChars = $maxChars - $indent;
!empty( $first_line_begin )
$out = substr( $out, ( $first_line_begin * - 1 ) );
* Cuts the given text that the length is not longer than $max_chars
* characters. The last three characters will be "...".
* @author Daniel Plücken <daniel@debakel.net>
* @param string $text The string which should be modified.
$inputstr = empty( $text ) ? "" : $text;
$maxChars = empty( $max_chars ) ? 15 : $max_chars;
$out = ( strlen( $inputstr ) + 3 > $maxChars )
? substr( $inputstr, 0, $maxChars - 3 ). "..."
* Replaces special characters to entities. This is very useful for embed
* @author Daniel Plücken <daniel@debakel.net>
* @param string $text The string which should be modified.
$outputString = str_replace( "'", "′", $outputString );
$outputString = str_replace( "<", "<", $outputString );
$outputString = str_replace( ">", ">", $outputString );
* Replaces entities to special characters. This is very useful for handle
* @author Daniel Plücken <daniel@debakel.net>
* @param string $text The string which should be modified.
$outputString = str_replace( "′", "'", $outputString );
$outputString = str_replace( "<", "<", $outputString );
$outputString = str_replace( ">", ">", $outputString );
* @author Daniel Plücken <daniel@debakel.net>
* @param string $str_in The string which should be modified.
"!^\s*(SELECT|INSERT|UPDATE)\s+!is",
$str_fieldname_pattern = "(?:`[^`]+?`|[^- .+`;'=\\\\()*]+?)";
"!^ SELECT (.+?)FROM.+!is",
"!((?:(?:DISTINCT\s+)?[-_a-z0-9]*\.?". $str_fieldname_pattern. "|". SINGLE_QUOTATION_STRING. ")(?:\s+AS\s+". $str_fieldname_pattern. ")?\s*,)\s*!is",
"!^ SELECT (?:.+?)FROM(.+?)(?:LEFT\s+JOIN|RIGHT\s+JOIN|NATURAL\s+JOIN|WHERE|ORDER\s+BY|GROUP\s+BY).*!is",
"!((?:[-_a-z0-9]*\.?". $str_fieldname_pattern. "|". SINGLE_QUOTATION_STRING. ")(?:\s+AS\s+". $str_fieldname_pattern. ")?\s*,)\s*!is",
"!^ SELECT (?:.+?)FROM(?:.+?)(LEFT\s+JOIN|RIGHT\s+JOIN|NATURAL\s+JOIN|WHERE|ORDER\s+BY|GROUP\s+BY)!is",
" SELECT ". $fields_of_select. "\r\n"
. " FROM ". $tables_of_select. "$1", $out
$out = preg_replace( "!^\s*REPLACE +!is", " REPLACE ", $out );
"#\s+(?<!LEFT|RIGHT|NATURAL)\s+JOIN +#is",
$out = preg_replace( "!\s+LEFT\s+JOIN +!is", " \r\n LEFT JOIN ", $out );
$out = preg_replace( "!\s+RIGHT\s+JOIN +!is", " \r\n RIGHT JOIN ", $out );
$out = preg_replace( "!\s+NATURAL\s+JOIN +!is", " \r\nNATURAL JOIN ", $out );
$out = preg_replace( "!\s+FROM +!is", " \r\n FROM ", $out );
$out = preg_replace( "!\s+(OR|ON) +!is", " \r\n $1 ", $out );
$out = preg_replace( "!\s+WHERE +!is", " \r\n WHERE ", $out );
$out = preg_replace( "!\s+(ORDER\s+BY|GROUP\s+BY) +!is", " \r\n $1 ", $out );
* Replaces character in that kind, that only html-character remain.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $inputstr The string which should be modified.
* Replaces character in that kind, that only ascii-character remain.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $inputstr The string which should be modified.
* Replaces each in series appearing whitespaces inside a textarea to only
* @author Daniel Plücken <daniel@debakel.net>
* @param string $string The string which should be modified.
"<textarea$1>[-#BEGIN#-]",
while( $pos_begin = strpos( $outputstr, "[-#BEGIN#-]", $pos_begin ) )
$pos_end = strpos( $outputstr, "[-#END#-]", $pos_begin );
$pos_end - $pos_begin - 11
$tmp_rep_str = trim( $tmp_str );
$tmp_rep_str = str_replace( "\r\n", "\n", $tmp_rep_str );
$tmp_rep_str = str_replace( "\r" , "\n", $tmp_rep_str );
$tmp_rep_str = str_replace( "\n " , "\n", $tmp_rep_str );
"[-#BEGIN#-]". $tmp_str. "[-#END#-]",
* Sets the indent of all lines of a string if the function "breakBy",
* "format" or "blockqoutePlainText" is used and no indent is given by
* @author Daniel Plücken <daniel@debakel.net>
* Sets the maximum of characters that should be in a line if the function
* "breakBy", "format" or "blockqoutePlainText" is used and no indent is
* @author Daniel Plücken <daniel@debakel.net>
} // End of class PlainTextFormatter
|