Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: PHP | Size: 0.78 KB | Hits: 32 | Expires: Never
Copy text to clipboard
  1. function AddColorCodes($str){
  2.         $out='';
  3.         $colors = array('0'=>'000',
  4.                         '1'=>'008',
  5.                         '2'=>'00f',
  6.                         '3'=>'090',
  7.                         '4'=>'099',
  8.                         '5'=>'0f0',
  9.                         '6'=>'0ff',
  10.                         '7'=>'800',
  11.                         '8'=>'808',
  12.                         '9'=>'990',
  13.                         'a'=>'888',
  14.                         'b'=>'ccc',
  15.                         'c'=>'f00',
  16.                         'd'=>'f0f',
  17.                         'e'=>'ff0',
  18.                         'f'=>'fff' );
  19.         $offset=0;
  20.         $color='';
  21.  
  22.         while( ($p = strpos($str,'&',$offset))!==FALSE ){
  23.                
  24.                 if( $p>strlen($str)-3 ) break;
  25.  
  26.                 $out .= substr($str,$offset,$p-$offset);
  27.                 $tc = $str[$p+1];
  28.                        
  29.                 if( array_key_exists($tc,$colors) ){
  30.                         if($color!='') $out .= '</span>';
  31.                         $out .='<span style="color:#'.$colors[$tc].'">';
  32.                         $color = $tc;
  33.                 }
  34.                 $offset = $p + 2;
  35.         }
  36.  
  37.         $out .= substr($str,$offset);
  38.         if($color!=''){
  39.                 $out .= '</span>';
  40.         }else{
  41.                 $out = $str;
  42.         }
  43.         return $out;
  44. }