Mistři v programování na #hovnokod

PHP #900

Prevadime znakove sady...

// prevodni vektory 8-bit
$vector['kam'] = '›†’šŸ‘œŒ«ŽŠ€‰‹…¥•§™žŠ—šª „‡‚ˆ¡ƒ€¢“”©–£˜cC';
$vector['win'] = 'ŠŽšžŒŸÀÁÄÅÈÉÌÍÏÒÓÔÖØÙÚÜÝàáäåèéìíïòóôöøùúüýæÆ';
$vector['asc'] = 'STZstzLlRAALCEEIDNOOORUUUYraalceeidnoooruuuycC';
$vector['pl2'] = '曊眧•–赎‘¬·ÖÒÕàâ™üÞéšíꠄ’Ÿ‚Ø¡Ô墓”ý…£ìcC';
$vector['il2'] = '©«®¹»Ÿ¥µÀÁÄÅÈÉÌÍÏÒÓÔÖØÙÚÜÝàáäåèéìíïòóôöøùúüýæÆ';
$vector['utf8'] = "ŠŀŜšťşĜğŔÁÄĹČÉĚÍĎŇÓÔÖŘŮÚÜÝŕáÀĺčéěíďňóÎöřůúÌÜćĆ";

// € => €

// mapovaci pole nazvu kodovani pro nase potreby
$charset2vector = array(
  'iso-8859-2'   => 'il2',
  'windows-1250' => 'win',
  'us-ascii'     => 'asc',	
  'utf-8'        => 'utf8',
  );

// hlavni funkce
function cz2cz($s, $inv = "win", $outv = "il2", $xslash = false) {
  global $vector;
  if (is_array($s)):
    foreach ($s as $key=>$val):
      $s[$key] = cz2cz($val, $inv, $outv, $xslash);
    endforeach;
    return $s; 
  else:
    if ($xslash) $s = xslash_decode($s);
    if (($inv==$outv)):
      return $s;
    elseif ($outv=="utf8"):
      return any2utf($s, $inv);
    elseif ($inv=="utf8"):
      return utf2any($s, $outv);
    else:                                            
      $res = strtr($s, $vector[$inv], $vector[$outv]);
      return $res;
    endif;
  endif;  
  }

// pomocne mezifunkce pro 16bit
function utf2any($s, $outv = "win") {
  global $vector;
  for ($i=0; $i<strlen($vector['utf8']); $i+=2):
    $u_char = substr($vector['utf8'], $i, 2);
    $w_char = $vector[$outv][$i/2];
    $s = str_replace($u_char, $w_char, $s);
  endfor;
  return $s;
  }

function any2utf($s, $inv = "win") {
  global $vector;
  for ($i=0; $i<strlen($s); $i++):
    $p = strpos($vector[$inv], $s[$i]);
    if (($p>0)||($i==0&&($vector[$inv][0])==($s[$i]))):
      $u_char = substr($vector['utf8'], $p*2, 2);
      $new_s .= $u_char;
    else:
      $new_s .= $s[$i];
    endif;
  endfor;
  return $new_s;
  }

// obcas pouzivane kodovani
function xslash_decode($s) {
  $loop_limit = 32;
  $counter = 0;
  while (ereg("\\x([a-f0-9]{2})", $s, $matches)):
    $s = str_replace('\x'.$matches[1], chr(hexdec($matches[1])), $s);
    $counter++;
    if ($counter>$loop_limit) break;
  endwhile;
  return $s;
  }

Adam Schubert,