Seiten

  • Startseite
  • Impressum
  • Inhalt
  • MINT
  • Sprache
  • Soziales
  • Geist
  • Kunst
  • Gemischtes
  • Gedichte

Donnerstag, 28. August 2014

xxx2string()-Methoden


Deutsch

Meiner Schwester zuliebe ist/war dieser Beitrag in neongrüner Schrift und auf transparentem Hintergrunde verfasst. :-)

Ich ar­bei­te der­zeit an ei­ner in­tel­li­gen­ten Such­ma­schi­ne (in PHP), die Da­ten aus al­len mei­nen Da­tei­en auf dem Rech­ner aus­liest und ver­wer­tet, und brau­che in die­sem Zu­sam­men­hang Funk­tio­nen, die Do­ku­men­te ver­schie­dens­ter For­ma­te (.doc, .docx, .html, .odt, .ods, .pdf, .ppt, .txt, .xls und .xml)[1] in rei­nen Text um­wan­deln. Und weil die­se Me­tho­den ganz sinn­voll sind, fol­gen sie nun (un­kom­men­tiert; wer ein biss­chen Ah­nung von In­fo hat, wird sie ver­ste­hen, und der Rest schau­te sich mei­ne Er­klärun­gen wohl so­wie­so nicht an). Ich sag's gleich im Vor­hin­ein: Die Funk­tio­nen ex­tra­hie­ren den Klar­text mal mehr und mal we­ni­ger gut, was so­wohl vom For­ma­te als auch von der Da­tei abhängt; ich tes­te im­mer noch an ih­nen her­um.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
    function file2string($file){
        $filea = explode(".", $file);
        $type = $filea[count($filea)-1];
        $string = "";
        switch($type){
            case "doc": $string = doc2string($file); break;
            case "docx": $string = docx2string($file); break;
            case "html": $string = html2string($file); break;
            case "odt": $string = odt2string($file); break;
            case "ods": $string = ods2string($file); break;
            case "pdf": $string = pdf2string($file); break;
            case "ppt": $string = ppt2string($file); break;
            case "txt": $string = txt2string($file); break;            
            case "xls": $string = xls2string($file); break;
            case "xml": $string = xml2string($file); break;
        }
        return $string;
    }
    function doc2string($source){
        $str = file_get_contents($source);
        return $str;
    }
    function docx2string($source){
        $zip = new ZipArchive;
        $zip->open($source);
        $index = $zip->locateName('word/document.xml');
        $data = $zip->getFromIndex($index);
        $zip->close();
        $sxe = new SimpleXMLElement($data);
        $text = $sxe->xpath("//w:t");
        $text2 = "";
        foreach($text as $t)
        $text2 .= $t;
        return utf8_decode($text2);
    }
    function html2string($source){
        $source = file_get_contents($source);
        $source = html_entity_decode($source);
        $source = strip_tags($source);
        return utf8_decode($source);
    }
    function ods2string($source){
        $zip = new ZipArchive;
        $zip->open($source);
        $index = $zip->locateName('content.xml');
        $data = $zip->getFromIndex($index);
        $zip->close();
        $d = "‽";
        $data = str_replace("><", ">$d<", $data);
        $data = strip_tags($data);
        while(str_replace("$d$d", "$d", $data) !== $data){
            $data = str_replace("$d$d", "$d", $data);
        }
        $data = str_replace("$d", " ", $data);
        return utf8_decode($data);
    }
    function odt2string($source){
        $zip = new ZipArchive;
        $zip->open($source);
        $index = $zip->locateName('content.xml');
        $data = $zip->getFromIndex($index);
        $zip->close();
        return utf8_decode(strip_tags($data));
    }
    /**
     * @source: http://php.net/manual/de/ref.pdf.php#56492
     */
    function pdf2string($source){
        $fp = fopen($source, 'rb');
        $content = fread($fp, filesize($source));
        fclose($fp);
        $searchstart = 'stream';
        $searchend = 'endstream';
        $pdfText = '';
        $pos = 0;
        $pos2 = 0;
        $startpos = 0;
        while($pos !== false && $pos2 !== false){
            $pos = strpos($content, $searchstart, $startpos);
            $pos2 = strpos($content, $searchend, $startpos+1);
            if ($pos !== false && $pos2 !== false){
                if ($content[$pos] == 0x0d && $content[$pos+1] == 0x0a){
                    $pos += 2;
                }
                else if($content[$pos] == 0x0a){
                    $pos++;
                }
                if($content[$pos2-2] == 0x0d && $content[$pos2-1] == 0x0a){
                    $pos2 -= 2;
                }
                else if($content[$pos2-1] == 0x0a){
                    $pos2--;
                }
                $textsection = substr($content, $pos+strlen($searchstart)+2, $pos2-$pos-strlen($searchstart)-1);
                $data = @gzuncompress($textsection);
                $pdfText .= pdfExtractText($data);
                $startpos = $pos2 + strlen($searchend)-1;
            }
        }
        return preg_replace('/(\s)+/', ' ', $pdfText);
    }
    function ppt2string($source){
        $str = file_get_contents($source);
        if(strlen($str) < 3000000){
            return entferne_buchstabensalat($str);
        }
        else {
            return "";
        }
    }
    function txt2string($source){
        return file_get_contents($source);
    }
    function xml2string($source){
        $source = file_get_contents($source);
        $source = html_entity_decode($source);
        $source = strip_tags($source);
        return utf8_decode($source);
    }
    function xls2string($source){
        include_once 'phpexcel-master/Classes/PHPExcel/IOFactory.php';
        $objPHPExcel = PHPExcel_IOFactory::load($source);
        $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
        $output = "";
        $output .= "<table>";
        for($i = 1; $i < count($sheetData); $i++){
            $output .= "<tr>";
            for($j = 65; $j < 65+count($sheetData[$i]); $j++){
                $output .= "<td>".$sheetData[$i][chr($j)]."</td>";
            }
            $output .= "</tr>";
        }
        $d = "‽";
        $output = str_replace("><", ">$d<", $output);
        $output = strip_tags($output);
        $output = str_replace($d, " ", $output);
        unset($objPHPExcel);
        return utf8_decode($output);
    }
    function pdfExtractText($psData){
        if(!is_string($psData)){
            return '';
        }
        $text = '';
        $psData = str_replace('\)', '##ENDBRACKET##', $psData);
        $psData = str_replace('\]', '##ENDSBRACKET##', $psData);
        preg_match_all('/(T[wdcm*])[\s]*(\[([^\]]*)\]|\(([^\)]*)\))[\s]*Tj/si', $psData, $matches);
        for($i = 0; $i < sizeof($matches[0]); $i++){
            if($matches[3][$i] != ''){
                preg_match_all('/\(([^)]*)\)/si', $matches[3][$i], $subMatches);
                foreach ($subMatches[1] as $subMatch){
                    $text .= $subMatch;
                }
            }
            else if ($matches[4][$i] != ''){
                $text .= ($matches[1][$i] == 'Tc' ? ' ' : '') . $matches[4][$i];
            }
        }
        $trans = array( 
            '...'               =>    '…', 
            '\205'              =>    '…', 
            '\221'              =>    chr(145), 
            '\222'              =>    chr(146), 
            '\223'              =>    chr(147), 
            '\224'              =>    chr(148), 
            '\226'              =>    '-', 
            '\267'              =>    '•', 
            '\('                =>    '(', 
            '\['                =>    '[', 
            '##ENDBRACKET##'    =>    ')', 
            '##ENDSBRACKET##'   =>    ']', 
            chr(133)            =>    '-', 
            chr(141)            =>    chr(147), 
            chr(142)            =>    chr(148), 
            chr(143)            =>    chr(145), 
            chr(144)            =>    chr(146), 
        );
        $text = strtr($text, $trans);
        return $text;
    }
    function entferne_buchstabensalat($string){
        $array = array();
        for($i = 33; $i < 126; $i++){
            $array[] = chr($i);
        }
        $l = 10000;
        $m = 6000;
        $k = 0;
        $tilde = "";
        for($i = 0; $i < $l; $i++){
            $tilde .= "~";
        }
        while($k < 4){
            for($n = 0; $n < floor(strlen($string))/$l; $n++){
                $block = substr($string, $n*$l, $l);
                if(strlen(str_replace($array, "", $block)) > $m){
                    $string = substr($string, 0, $n*$l).$tilde.substr($string, ($n*$l)+$l);
                }
            }
            $string = str_replace($tilde, "", $string);
            $k++;
            $l = $l / 10;
            $m = $m / 10;
            $tilde = "";
            for($i = 0; $i < $l; $i++){
                $tilde .= "~";
            }
        }
        return $string;
    }
?>

[1].doc = Word Do­cu­ment
.docx = Of­fi­ce Open XML Do­cu­ment
.html = Hy­per­Text Mar­kup Lan­gua­ge
.odt = Open­Do­cu­ment Text
.ods = Open­Do­cu­ment Spreads­heet
.pdf = Por­ta­ble Do­cu­ment For­mat
.ppt = Po­wer­Point Pre­sen­ta­ti­on
.txt = Text fi­le
.xls = Ex­cel Spreads­heet
.xml = Ex­ten­si­ble Mar­kup Lan­gua­ge

Keine Kommentare:

Kommentar veröffentlichen