Categorias

Converter arquivo DOC ou PPT para texto puro

As seguintes funções convertem documentos do Office (.DOC ou .PPT) para texto puro, usando linguagem PHP.

Função DOC2Text:

/*** Converte DOC para Texto Limpo*
@param filename: Caminho do
arquivo*/
function DOC2Text($filename) { $fileHandle = fopen($filename, “r”); $line = @fread($fileHandle, filesize($filename)); $lines = explode(chr(0x0D),$line); $outtext = “”; foreach($lines as $thisline) { $pos = strpos($thisline, chr(0x00)); if (($pos !== FALSE)||(strlen($thisline)==0)) { } else { $outtext .= $thisline.” “; } } $outtext = preg_replace(“/[^a-zA-Z0-9s,.-nrt@/_()]/”,””,$outtext); return $outtext;}

Modo de Uso:

$doc_content = DOC2Text(‘/tmp/meudoc.doc’);
echo “$doc_content”;

Função PPT2Text:

/*** Converte PPT para Texto Limpo*
@param filename: Caminho do
arquivo*/

function PPT2Text($filename) {
// Esta abordagem utiliza a detecção da string “chr(0f).Hex_value.chr(0x00).chr(0x00).chr(0x00)” para identificar strings de texto, que são então fechadas com outro NUL chr(0x00) e captura o texto entre os delimitadores
$fileHandle = fopen($filename, “r”);
$line = @fread($fileHandle, filesize($filename));
$lines = explode(chr(0x0f),$line);
$outtext = ”;

foreach($lines as $thisline) {
if (strpos($thisline, chr(0x00).chr(0x00).chr(0x00)) == 1) {
$text_line = substr($thisline, 4);
$end_pos = strpos($text_line, chr(0x00));
$text_line = substr($text_line, 0, $end_pos);
$text_line = preg_replace(“/[^a-zA-Z0-9s,.-nrt@/_()]/”,””,$text_line);
if (strlen($text_line) > 1) {
$outtext.= substr($text_line, 0, $end_pos).”n”;
}
}
}
return $outtext;
}

Modo de Uso:

$ppt_content = PPT2Text(‘/tmp/meuppt.ppt’);
echo “$ppt_content”;

function DOC2Text($filename) {    $fileHandle = fopen($filename, "r");    $line = @fread($fileHandle, filesize($filename));       $lines = explode(chr(0x0D),$line);    $outtext = "";    foreach($lines as $thisline)      {        $pos = strpos($thisline, chr(0x00));        if (($pos !== FALSE)||(strlen($thisline)==0))          {          } else {            $outtext .= $thisline." ";          }      }     $outtext = preg_replace("/[^a-zA-Z0-9s,.-nrt@/_()]/","",$outtext);    return $outtext;} 

function PPT2Text($filename) {
    // Esta abordagem utiliza a detecção da string "chr(0f).Hex_value.chr(0x00).chr(0x00).chr(0x00)" para identificar strings de texto, que são então fechadas com outro NUL chr(0x00) e captura o texto entre os delimitadores
    $fileHandle = fopen($filename, "r");
    $line = @fread($fileHandle, filesize($filename));
    $lines = explode(chr(0x0f),$line);
    $outtext = '';
   
    foreach($lines as $thisline) {
        if (strpos($thisline, chr(0x00).chr(0x00).chr(0x00)) == 1) {
            $text_line = substr($thisline, 4);
            $end_pos   = strpos($text_line, chr(0x00));
            $text_line = substr($text_line, 0, $end_pos);
            $text_line = preg_replace("/[^a-zA-Z0-9s,.-nrt@/_()]/","",$text_line);
            if (strlen($text_line) > 1) {
                $outtext.= substr($text_line, 0, $end_pos)."n";
            }
        }
    }
    return $outtext;
}