Zeichensätze

Computer kennen nur Zahlen. Warum wir trotzdem Zeichen sehen, liegt daran, dass es Definitionen gibt, die bestimmten Zahlen Zeichen zuordnen.  Zeichensatz bei Wikipedia.org.

PHP Zeichensatz-Testprogramm

In PHP gibt es in den  Multibyte-Erweiterungen sehr schöne Funktionen, die helfen können, wenn man sich nicht sicher ist, in welchem Zeichensatz ein Dokument vorliegt.

Mein Testprogramm:

<?php
$file = $argv[1];

$fp = fopen($file, 'r');
while($data = fgets($fp, 1024)){
    $contents .= $data;
}
fclose($fp);
$ary[] = "UTF-8";
$ary[] = "ISO-8859-15";
$ary[] = "ISO-8859-1";
$ary[] = "ASCII";

if(function_exists(mb_detect_encoding)){
    if(mb_detect_encoding($contents, $ary) == 'UTF-8'){
        echo "$file is utf-8";
    } else {

        echo "$file is not in utf-8! Detected: " .
        mb_detect_encoding($contents, $ary);
    }
}
echo "\nregtest:\n";
    if(preg_match('%^(?:
                     [\x09\x0A\x0D\x20-\x7E]           # ASCII
                   | [\xC2-\xDF][\x80-\xBF]            # non-overlong 2-byte
                   |  \xE0[\xA0-\xBF][\x80-\xBF]       # excluding overlongs
                   | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
                   |  \xED[\x80-\x9F][\x80-\xBF]       # excluding surrogates
                   |  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
                   | [\xF1-\xF3][\x80-\xBF]{3}         # planes 4-15
                   |  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
               )*$%xs', $contents)){
    echo 'file is utf-8!' . "\n";
               } else { echo "file is not utf-8\n"; }

echo "\n";
?>
php -q utf8_test.php testISO.txt

testISO.txt is not in utf-8! Detected: ISO-8859-15
regtest:
file is not utf-8

Links zu Zeichensätzen