ultimix
text.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
26  class text_1_0_0{
27 
38  var $Tags = false;
39 
50  function __construct()
51  {
52  try
53  {
54  $this->Tags = get_package( 'string::tags' , 'last' , __FILE__ );
55  }
56  catch( Exception $e )
57  {
58  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
59  }
60  }
61 
88  function explode_into_words( $String , $MaxWordLength = 80 )
89  {
90  try
91  {
92  $RawWords = strip_tags( $this->Tags->compile_phpbb_tags( $String ) );
93  $RawWords = str_replace( "\r" , ' ' , $RawWords );
94  $RawWords = str_replace( "\n" , ' ' , $RawWords );
95  $RawWords = explode( ' ' , $RawWords );
96 
97  foreach( $RawWords as $Key => $Value )
98  {
99  if( strlen( $Value ) > $MaxWordLength )
100  {
101  $Parts = chunk_split( $Value , $MaxWordLength , '[br]' );
102  $String = str_replace( $Value , $Parts , $String );
103  }
104  }
105 
106  return( $String );
107  }
108  catch( Exception $e )
109  {
110  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
111  }
112  }
113 
136  function remove_bom( $Str )
137  {
138  try
139  {
140  if( $Str !== false && strlen( $Str ) )
141  {
142  if( ord( @$Str[ 0 ] ) === 239 && ord( @$Str[ 1 ] ) === 187 && ord( @$Str[ 2 ] ) === 191 )
143  {
144  $Str = substr( $Str , 3 );
145  }
146  }
147 
148  return( $Str );
149  }
150  catch( Exception $e )
151  {
152  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
153  }
154  }
155 
178  function common_prefix( &$Strings )
179  {
180  try
181  {
182  if( isset( $Strings[ 0 ] ) === false )
183  {
184  return( '' );
185  }
186 
187  $CommonPrefix = '';
188  $MaxLength = strlen( $Strings[ 0 ] );
189  for( $i = 0 ; $i < $MaxLength ; $i++ )
190  {
191  foreach( $Strings as $j => $String )
192  {
193  if( $Strings[ 0 ][ $i ] != $String[ $i ] )
194  {
195  return( $CommonPrefix );
196  }
197  }
198  $CommonPrefix .= $Strings[ 0 ][ $i ];
199  }
200 
201  return( $CommonPrefix );
202  }
203  catch( Exception $e )
204  {
205  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
206  }
207  }
208 
235  function trim_common_prefix( &$Strings , $CommonPrefix = false )
236  {
237  try
238  {
239  if( $CommonPrefix === false )
240  {
241  $CommonPrefix = $this->common_prefix( $Strings );
242  }
243 
244  if( $CommonPrefix !== '' )
245  {
246  foreach( $Strings as $i => $String )
247  {
248  $Strings[ $i ] = str_replace( $CommonPrefix , '' , $String );
249  }
250  }
251 
252  return( $Strings );
253  }
254  catch( Exception $e )
255  {
256  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
257  }
258  }
259 
270  var $WINPatternL = '~([\270])|([\340-\347])|([\350-\357])|([\360-\367])|([\370-\377])~s';
271 
282  var $WINPatternU = '~([\250])|([\300-\307])|([\310-\317])|([\320-\327])|([\330-\337])~s';
283 
294  var $KOIPatternL = '~([\243])|([\300-\307])|([\310-\317])|([\320-\327])|([\330-\337])~s';
295 
306  var $KOIPatternU = '~([\263])|([\340-\347])|([\350-\357])|([\360-\367])|([\370-\377])~s';
307 
318  var $Patterns = array();
319 
334  private function init_basic_patterns()
335  {
336  try
337  {
338  $this->Patterns[ 'search_l_k' ] = $this->KOIPatternL;
339  $this->Patterns[ 'search_U_k' ] = $this->KOIPatternU;
340  $this->Patterns[ 'search_l_w' ] = $this->WINPatternL;
341  $this->Patterns[ 'search_U_w' ] = $this->WINPatternU;
342  $this->Patterns[ 'search_l_u' ] = "~([\xD1\x91])|([\xD1\x80-\x8F])|([\xD0\xB0-\xBF])~s";
343  $this->Patterns[ 'search_U_u' ] = "~([\xD0\x81])|([\xD0\x90-\x9F])|([\xD0\xA0-\xAF])~s";
344  }
345  catch( Exception $e )
346  {
347  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
348  }
349  }
350 
365  private function init_iso()
366  {
367  try
368  {
369  $this->Patterns[ 'search_l_i' ] = '~([\361])|([\320-\327])|([\330-\337])|([\340-\347])|([\350-\357])~s';
370  $this->Patterns[ 'search_U_i' ] = '~([\241])|([\260-\267])|([\270-\277])|([\300-\307])|([\310-\317])~s';
371  }
372  catch( Exception $e )
373  {
374  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
375  }
376  }
377 
392  private function init_mac()
393  {
394  try
395  {
396  $this->Patterns[ 'search_l_m' ] = '~([\336])|([\340-\347])|([\350-\357])|'.
397  '([\360-\367])|([\370-\370])|([\337])~s';
398  $this->Patterns[ 'search_U_m' ] = '~([\335])|([\200-\207])|([\210-\217])|([\220-\227])|([\230-\237])~s';
399  }
400  catch( Exception $e )
401  {
402  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
403  }
404  }
405 
420  private function init_ibm_1()
421  {
422  try
423  {
424  $this->Patterns[ 'search_l_c' ] = '~([\204])|([\234])|([\236])|([\240])|([\242])|([\244])|([\246])|'.
425  '([\250])|([\252])|([\254])|([\265])|([\267])|([\275])|([\306])|'.
426  '([\320])|([\322])|([\324])|([\326])|([\330])|([\340])|([\341])|'.
427  '([\343])|([\345])|([\347])|([\351])|([\353])|([\355])|([\361])|'.
428  '([\363])|([\365])|([\367])|([\371])|([\373])~s';
429  }
430  catch( Exception $e )
431  {
432  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
433  }
434  }
435 
450  private function init_ibm_2()
451  {
452  try
453  {
454  $this->Patterns[ 'search_U_c' ] = '~([\205])|([\235])|([\237])|([\241])|([\243])|([\245])|([\247])|'.
455  '([\251])|([\253])|([\255])|([\266])|([\270])|([\276])|([\307])|'.
456  '([\321])|([\323])|([\325])|([\327])|([\335])|([\336])|([\342])|'.
457  '([\344])|([\346])|([\350])|([\352])|([\354])|([\356])|([\362])|'.
458  '([\364])|([\366])|([\370])|([\372])|([\374])~s';
459  }
460  catch( Exception $e )
461  {
462  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
463  }
464  }
465 
480  private function init_ibm_3()
481  {
482  try
483  {
484  $this->Patterns[ 'search_l_a' ] = '~([\361])|([\240-\247])|([\250-\257])|([\340-\347])|([\350-\357])~s';
485  $this->Patterns[ 'search_U_a' ] = '~([\360])|([\200-\207])|([\210-\217])|([\220-\227])|([\230-\237])~s';
486  }
487  catch( Exception $e )
488  {
489  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
490  }
491  }
492 
507  private function init_patterns()
508  {
509  try
510  {
511  $this->init_basic_patterns();
512 
513  $this->init_iso();
514  $this->init_mac();
515  $this->init_ibm_1();
516  $this->init_ibm_2();
517  }
518  catch( Exception $e )
519  {
520  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
521  }
522  }
523 
546  private function get_enc_regexp( $Name )
547  {
548  try
549  {
550  if( isset( $this->Patterns[ $Name ] ) )
551  {
552  return( $this->Patterns[ $Name ] );
553  }
554 
555  throw( new Exception( 'Illegal pattern name' ) );
556  }
557  catch( Exception $e )
558  {
559  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
560  }
561  }
562 
585  private function analize_detection_results( $Charsets )
586  {
587  try
588  {
589  arsort( $Charsets );
590  $key = key( $Charsets );
591 
592  if( max( $Charsets ) == 0 )
593  {
594  return( 'utf-8' );
595  }
596 
597  switch( $key )
598  {
599  case( 'w' ):return( 'cp1251' );
600  case( 'k' ):return( 'koi8-r' );
601  case( 'i' ):return( 'iso-8859-5' );
602  case( 'm' ):return( 'x-mac-cyrillic' );
603  case( 'a' ):return( 'ibm866' );
604  case( 'c' ):return( 'ibm855' );
605  case( 'u' ):return( 'utf-8' );
606  }
607 
608  return( 'utf-8' );
609  }
610  catch( Exception $e )
611  {
612  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
613  }
614  }
615 
642  private function try_encoding( &$Charsets , $Content , $TryCharset )
643  {
644  try
645  {
646  $Result = preg_match_all(
647  $this->get_enc_regexp( "search_l_$TryCharset" ) , $Content , $Arr , PREG_PATTERN_ORDER
648  );
649 
650  if( $Result )
651  {
652  $Charsets[ $TryCharset ] += count( $Arr[ 0 ] ) * 3;
653  }
654 
655  $Result = preg_match_all(
656  $this->get_enc_regexp( "search_U_$TryCharset" ) , $Content , $Arr , PREG_PATTERN_ORDER
657  );
658 
659  if( $Result )
660  {
661  $Charsets[ $TryCharset ] += count( $Arr[ 0 ] );
662  }
663  }
664  catch( Exception $e )
665  {
666  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
667  }
668  }
669 
692  function detect_encoding( $Content )
693  {
694  try
695  {
696  $Charsets = array ( 'w' => 0, 'k' => 0, 'i' => 0, 'm' => 0, 'a' => 0, 'c' => 0, 'u' => 0 );
697 
698  $this->try_encoding( $Charsets , $Content , 'w' );
699 
700  $this->try_encoding( $Charsets , $Content , 'k' );
701 
702  $this->try_encoding( $Charsets , $Content , 'i' );
703 
704  $this->try_encoding( $Charsets , $Content , 'm' );
705 
706  $this->try_encoding( $Charsets , $Content , 'a' );
707 
708  $this->try_encoding( $Charsets , $Content , 'c' );
709 
710  $this->try_encoding( $Charsets , $Content , 'u' );
711 
712  return( $this->analize_detection_results( $Charsets ) );
713  }
714  catch( Exception $e )
715  {
716  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
717  }
718  }
719 
754  function iconv( $InCharset , $OutCharset , $Data )
755  {
756  try
757  {
758  if( is_array( $Data ) || is_object( $Data ) )
759  {
760  foreach( $Data as $Key => $Value )
761  {
762  set_field( $Data , $Key , $this->iconv( $InCharset , $OutCharset , $Value ) );
763  }
764  return( $Data );
765  }
766  else
767  {
768  if( $InCharset === false )
769  {
770  $InCharset = $this->detect_encoding( $Data );
771  }
772  return( @iconv( $InCharset , $OutCharset , $Data ) );
773  }
774  }
775  catch( Exception $e )
776  {
777  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
778  }
779  }
780  }
781 ?>