Text.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 // ---------------------------------------------------------------------------
00003 // $Id$
00004 // ---------------------------------------------------------------------------
00005 // DaCMS Content Management System
00006 // Copyright (C) 2002 Jan Dankert, jandankert@jandankert.de
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 // ---------------------------------------------------------------------------
00022 // $Log$
00023 // Revision 1.10  2009-04-18 00:56:14  dankert
00024 // Beim Verarbeiten von if-empty-Bereichen der Seitenvorlage keine regulären Ausdrücke mehr verwenden (da Binärausgaben wie PDF-Dokumente dabei beschädigt werden).
00025 //
00026 // Revision 1.9  2007-11-27 21:16:33  dankert
00027 // Bugfix in "encodeHtmlSpecialChars()"
00028 //
00029 // Revision 1.8  2007-11-27 21:10:35  dankert
00030 // Verschieben von "replaceHtmlChars()" nach Klasse Text.
00031 //
00032 // Revision 1.7  2007-11-24 13:22:04  dankert
00033 // Neue Methode "encodeHtmlSpecialChars()"
00034 //
00035 // Revision 1.6  2007-11-17 13:36:06  dankert
00036 // Methode "textdiff()" in Text-Klasse verschoben.
00037 //
00038 // Revision 1.5  2006/12/09 16:56:40  dankert
00039 // Methode "encodeHtml()" ersetzt nun auch Umlaute gem. Konfiguration.
00040 //
00041 // Revision 1.4  2005/04/16 22:26:15  dankert
00042 // Erweiterung Methode maxLength()
00043 //
00044 // Revision 1.3  2005/02/17 21:22:22  dankert
00045 // Weitere Funktionen f?r HTML und BB-Code
00046 //
00047 // Revision 1.2  2004/05/02 15:04:16  dankert
00048 // Einf�gen package-name (@package)
00049 //
00050 // Revision 1.1  2004/04/24 17:03:28  dankert
00051 // Initiale Version
00052 //
00053 // ---------------------------------------------------------------------------
00054 
00055 
00062 class Text
00063 {
00069      function maxLaenge( $laenge,$text )
00070      {
00071           return Text::maxLength($text,$laenge);
00072      }
00073 
00074 
00085      function maxLength( $text,$laenge=20,$append='...',$where=STR_PAD_RIGHT )
00086      {
00087           if   ( strlen($text) > $laenge )
00088           {
00089                if   ( $where == STR_PAD_RIGHT )
00090                     $text = substr($text,0,$laenge).$append;
00091                elseif    ( $where == STR_PAD_BOTH )
00092                     $text = substr($text,0,$laenge/2).$append.substr($text,strlen($text)-($laenge/2));
00093           }
00094 
00095           return $text;
00096      }
00097 
00098 
00106      function bbCode2Wiki( $inhalt )
00107      {
00108           $inhalt = eregi_replace('\[b\]([^\[]*)\[\/b\]'       , '*\\1*' ,$inhalt);
00109           $inhalt = eregi_replace('\[i\]([^\[]*)\[\/i\]'       , '_\\1_' ,$inhalt);
00110           $inhalt = eregi_replace('\[code\]([^\[]*)\[\/code\]' , '=\\1=' ,$inhalt);
00111 
00112           $inhalt = eregi_replace('\[url\]([^\[]*)[\/url\]'          ,'"\\1"->"\\1"' ,$inhalt);
00113           $inhalt = eregi_replace('\[url=([^\[]*)\]([^\[]*)\[\/url\]','"\\2"->"\\1"' ,$inhalt);
00114 
00115           return $inhalt;
00116      }
00117 
00118 
00126      function Html2Wiki( $inhalt )
00127      {
00128           $inhalt = eregi_replace('<b(.*)>(.*)</b>','*\\2*' ,$inhalt);
00129           $inhalt = eregi_replace('<i(.*)>(.*)</i>','_\\2_' ,$inhalt);
00130           $inhalt = eregi_replace('<a(.*)href="(.*)">(.*)</a>','"\\3"->"\\2"' ,$inhalt);
00131 
00132           return $inhalt;
00133      }
00134 
00135 
00142      function encodeHtml( $inhalt )
00143      {
00144           $inhalt = str_replace('&','&amp;',$inhalt);
00145           $inhalt = str_replace('<','&lt;' ,$inhalt);
00146           $inhalt = str_replace('>','&gt;' ,$inhalt);
00147 
00148           return $inhalt;
00149      }
00150 
00151      
00152      
00157      function replaceHtmlChars( $text )
00158      {
00159           global $conf;
00160           
00161           foreach( explode(' ',$conf['editor']['html']['replace']) as $repl )
00162           {
00163                list( $ersetze, $mit ) = explode(':',$repl.':');
00164                $text = str_replace($ersetze, $mit, $text);
00165           }
00166           
00167           return $text;
00168      }
00169      
00170      
00171      
00178      function encodeHtmlSpecialChars( $inhalt )
00179      {
00180           return Text::replaceHtmlChars( $inhalt );
00181      }
00182 
00183      
00184      
00189      function diff( $from_text,$to_text )
00190      {
00191           // Zaehler pro Textarray
00192           $pos_from = -1;
00193           $pos_to   = -1;
00194 
00195           // Ergebnis-Arrays
00196           $from_out = array();
00197           $to_out   = array();
00198 
00199           while( true )
00200           {
00201                $pos_from++;
00202                $pos_to  ++;
00203 
00204                if   ( !isset($from_text[$pos_from]) &&
00205                       !isset($to_text  [$pos_to  ]) )
00206                {
00207                     // Text in ist 'neu' und 'alt' zuende. Ende der Schleife.
00208                     break;
00209                }
00210                elseif
00211                     (  isset($from_text[$pos_from]) &&
00212                       !isset($to_text  [$pos_to]) )
00213                {
00214                     // Text in 'neu' ist zuende, die Restzeilen von 'alt' werden ausgegeben
00215                     while( isset($from_text[$pos_from]) )
00216                     {
00217                          $from_out[] = array('text'=>$from_text[$pos_from],'line'=>$pos_from+1,'type'=>'old');  
00218                          $to_out  [] = array();
00219                          $pos_from++;
00220                     }
00221                     break;  
00222                }
00223                elseif
00224                     ( !isset($from_text[$pos_from]) &&
00225                        isset($to_text  [$pos_to]) )
00226                {
00227                     // Umgekehrter Fall: Text in 'alt' ist zuende, Restzeilen aus 'neu' werden ausgegeben
00228                     while( isset($to_text[$pos_to]) )
00229                     {
00230                          $from_out[] = array();  
00231                          $to_out  [] = array('text'=>$to_text[$pos_to],'line'=>$pos_to+1,'type'=>'new');  
00232                          $pos_to++;
00233                     }
00234                     break;  
00235                }
00236                elseif
00237                     ( rtrim($from_text[$pos_from]) != rtrim($to_text[$pos_to]) )
00238                {
00239                     // Zeilen sind vorhanden, aber ungleich
00240                     // Wir suchen jetzt die naechsten beiden Zeilen, die gleich sind.
00241                     $max_entf = min(count($from_text)-$pos_from-1,count($to_text)-$pos_to-1);
00242 
00243                     #echo "suche start, max_entf=$max_entf, pos_from=$pos_from, pos_to=$pos_to<br/>";
00244                     
00245                     for ( $a=0; $a<=$max_entf; $a++ )
00246                     {
00247                          #echo "a ist $a<br/>";
00248                          for  ( $b=0; $b<=$max_entf; $b++ )
00249                          {
00250                               #echo "b ist $b<br/>";
00251                               if   ( trim($from_text[$pos_from+$b]) != '' &&
00252                                      $from_text[$pos_from+$b] == $to_text[$pos_to+$a] )
00253                               {
00254                                    $pos_gef_from = $pos_from+$b;
00255                                    $pos_gef_to   = $pos_to  +$a;
00256                                    break;
00257                               }
00258 
00259                               if   ( trim($from_text[$pos_from+$a]) != ''  &&
00260                                      $from_text[$pos_from+$a] == $to_text[$pos_to+$b] )
00261                               {
00262                                    $pos_gef_from = $pos_from+$a;
00263                                    $pos_gef_to   = $pos_to  +$b;
00264                                    break;
00265                               }
00266                          }
00267 
00268                          if   ( $b <=$max_entf)
00269                          {
00270                               break;
00271                          }
00272                     }
00273 
00274                     if   ( $a<=$max_entf )
00275                     {
00276                          // Gleiche Zeile gefunden
00277                          #echo "gefunden, pos_gef_from=$pos_gef_from, pos_gef_to=$pos_gef_to<br/>";
00278                          
00279                          if   ( $pos_gef_from - $pos_from == 0 )
00280                               $type = 'new';
00281                          elseif
00282                               ( $pos_gef_to - $pos_to == 0 )
00283                               $type = 'old';
00284                          else
00285                               $type = 'notequal';
00286 
00287                          while( $pos_gef_from - $pos_from > 0 &&
00288                                 $pos_gef_to   - $pos_to   > 0    )
00289                          {
00290                               $from_out[] = array('text'=>$from_text[$pos_from],'line'=>$pos_from+1,'type'=>$type);
00291                               $to_out  [] = array('text'=>$to_text  [$pos_to  ],'line'=>$pos_to+1  ,'type'=>$type);
00292                               
00293                               $pos_from++;
00294                               $pos_to++;
00295                          }
00296 
00297                          while( $pos_gef_from - $pos_from > 0 )
00298                          {
00299                               $from_out[] = array('text'=>$from_text[$pos_from],'line'=>$pos_from+1,'type'=>$type);
00300                               $to_out  [] = array();
00301                               $pos_from++;
00302                          }
00303 
00304                          while( $pos_gef_to - $pos_to   > 0 )
00305                          {
00306                               $from_out[] = array();
00307                               $to_out  [] = array('text'=>$to_text  [$pos_to  ],'line'=>$pos_to+1  ,'type'=>$type);
00308                               $pos_to++;
00309                          }
00310                          $pos_from--;
00311                          $pos_to--;
00312                     }
00313                     else
00314                     {
00315                          // Keine gleichen Zeilen gefunden
00316                          #echo "nicht gefunden, i=$i, j=$j, pos_from war $pos_from, pos_to war $pos_to<br/>";
00317 
00318                          while( true )
00319                          {
00320                               if   ( !isset($from_text[$pos_from]) &&
00321                                     !isset($to_text  [$pos_to  ]) )
00322                               {
00323                                    break;
00324                               }
00325                               elseif
00326                                    (  isset($from_text[$pos_from]) &&
00327                                      !isset($to_text  [$pos_to  ]) )
00328                               {
00329                                    $from_out[] = array('text'=>$from_text[$pos_from],'line'=>$pos_from+1,'type'=>'notequal');  
00330                                    $to_out  [] = array();
00331                               }
00332                               elseif
00333                                    ( !isset($from_text[$pos_from]) &&
00334                                       isset($to_text  [$pos_to  ]) )
00335                               {
00336                                    $from_out[] = array();  
00337                                    $to_out  [] = array('text'=>$to_text  [$pos_to  ],'line'=>$pos_to+1  ,'type'=>'notequal');
00338                               }
00339                               else
00340                               {
00341                                    $from_out[] = array('text'=>$from_text[$pos_from],'line'=>$pos_from+1,'type'=>'notequal');  
00342                                    $to_out  [] = array('text'=>$to_text  [$pos_to  ],'line'=>$pos_to+1  ,'type'=>'notequal');
00343                               }
00344                               $pos_from++;  
00345                               $pos_to++;
00346                          }
00347                     }
00348                }
00349                else
00350                {
00351                     // Zeilen sind gleich
00352                     $from_out[] = array('text'=>$from_text[$pos_from],'line'=>$pos_from+1,'type'=>'equal');  
00353                     $to_out  [] = array('text'=>$to_text  [$pos_to  ],'line'=>$pos_to+1  ,'type'=>'equal');  
00354                }
00355           }
00356           
00357           return( array($from_out,$to_out) );
00358      }
00359      
00360      
00361      function entferneVonBis($text,$von,$bis)
00362      {
00363           $beg = strpos($text,$von);
00364           $end = strpos($text,$bis);
00365           if   ( $beg!==false && $end !==false )
00366                $text = substr($src,0,$beg).substr($src,$end);
00367           return $text;
00368      }
00369 }
00370 
00371  
00372 ?>

Erzeugt am Thu May 14 00:55:49 2009 für OpenRat von  doxygen 1.5.8