PHP 处理 mht 文件

WBOY
Lepaskan: 2016-07-29 09:10:59
asal
1559 orang telah melayarinya
<span><?php </span><span>/**
</span><span> * Created by PhpStorm.
</span><span> * User: kungyu
</span><span> * Date: 2015/12/3
</span><span> * Time: 15:30
</span><span> */
</span><span>class </span>mhtparse {

    <span>var </span><span>$file </span><span>= </span><span>''</span><span>;
</span><span>var </span><span>$boundary </span><span>= </span><span>''</span><span>;
</span><span>var </span><span>$filedata </span><span>= </span><span>''</span><span>;
</span><span>var </span><span>$countparts </span><span>= </span><span>1</span><span>;
</span><span>var </span><span>$log </span><span>= </span><span>''</span><span>;
</span><span>function </span>extract() {
        <span>$this</span><span>-></span>read_filedata ()<span>;
</span><span>$this</span><span>-></span>file_parts ()<span>;
</span><span>return </span><span>1</span><span>;
</span>}

    <span>function </span>set_file(<span>$p</span>) {
        <span>$this</span><span>-></span>file <span>= </span><span>$p</span><span>;
</span>}

    <span>function </span>get_log() {
        <span>return </span><span>$this</span><span>-></span>log<span>;
</span>}

    <span>function </span>file_parts() {
        <span>$lines </span><span>= </span>explode ( <span>"</span><span>\n</span><span>"</span>, substr ( <span>$this</span><span>-></span>filedata, <span>0</span>, <span>8192 </span>) )<span>;
</span><span>foreach </span>( <span>$lines </span><span>as </span><span>$line </span>) {
            <span>$line </span><span>= </span>trim ( <span>$line </span>)<span>;
</span><span>if </span>(strpos ( <span>$line</span>, <span>'=' </span>) <span>!== </span><span>FALSE</span>) {
                <span>if </span>(strpos ( <span>$line</span>, <span>'boundary'</span>, <span>0 </span>) <span>!== </span><span>FALSE</span>) {
                    <span>$range </span><span>= </span><span>$this</span><span>-></span>getrange ( <span>$line</span>, <span>'"'</span>, <span>'"'</span>, <span>0 </span>)<span>;
</span><span>$this</span><span>-></span>boundary <span>= </span><span>"--" </span><span>. </span><span>$range </span><span>[</span><span>'range'</span><span>]</span><span>;
</span><span>$this</span><span>-></span>filedata <span>= </span>str_replace ( <span>$line</span>, <span>''</span>, <span>$this</span><span>-></span>filedata )<span>;
</span><span>break</span><span>;
</span>}
            }
        }
        <span>if </span>(<span>$this</span><span>-></span>boundary <span>!= </span><span>''</span>) {
            <span>$this</span><span>-></span>filedata <span>= </span>explode ( <span>$this</span><span>-></span>boundary, <span>$this</span><span>-></span>filedata )<span>;
</span><span>unset </span>( <span>$this</span><span>-></span>filedata <span>[</span><span>0</span><span>] </span>)<span>;
</span><span>$this</span><span>-></span>filedata <span>= </span>array_values ( <span>$this</span><span>-></span>filedata )<span>;
</span><span>$this</span><span>-></span>countparts <span>= </span>count ( <span>$this</span><span>-></span>filedata )<span>;
</span>} <span>else </span>{
            <span>$tmp </span><span>= </span><span>$this</span><span>-></span>filedata<span>;
</span><span>$this</span><span>-></span>filedata <span>= </span><span>array </span>(
                <span>$tmp
</span>)<span>;
</span>}
    }

    <span>function </span>get_all_part_file() {
        <span>return </span><span>$this</span><span>-></span>filedata<span>;
</span>}

    <span>function </span>get_part_to_file(<span>$i</span>) {
        <span>$line_dat<strong>a_star</strong>t </span><span>= </span><span>0</span><span>;
</span><span>$encoding </span><span>= </span><span>''</span><span>;
</span><span>$part_lines </span><span>= </span>explode ( <span>"</span><span>\n</span><span>"</span>, ltrim ( <span>$this</span><span>-></span>filedata <span>[</span><span>$i</span><span>] </span>) )<span>;
</span><span>foreach </span>( <span>$part_lines </span><span>as </span><span>$line_id </span>=> <span>$line </span>) {
            <span>$line </span><span>= </span>trim ( <span>$line </span>)<span>;
</span><span>if </span>(<span>$line </span><span>== </span><span>''</span>) {
                <span>if </span>(trim ( <span>$part_lines </span><span>[</span><span>0</span><span>] </span>) <span>== </span><span>'--'</span>)
                    <span>return </span><span>1</span><span>;
</span><span>$line_dat<strong>a_star</strong>t </span><span>= </span><span>$line_id</span><span>;
</span><span>break</span><span>;
</span>}
            <span>if </span>(strpos ( <span>$line</span>, <span>':' </span>) <span>!== </span><span>FALSE</span>) {
                <span>$pos </span><span>= </span>strpos ( <span>$line</span>, <span>':' </span>)<span>;
</span><span>$k </span><span>= </span>strtolower ( trim ( substr ( <span>$line</span>, <span>0</span>, <span>$pos </span>) ) )<span>;
</span><span>$v </span><span>= </span>trim ( substr ( <span>$line</span>, <span>$pos </span><span>+ </span><span>1</span>, strlen ( <span>$line </span>) ) )<span>;
</span><span>if </span>(<span>$k </span><span>== </span><span>'content-transfer-encoding'</span>) {
                    <span>$encoding </span><span>= </span><span>$v</span><span>;
</span>}
                <span>if </span>(<span>$k </span><span>== </span><span>'content-location'</span>) {
                    <span>$location </span><span>= </span><span>$v</span><span>;
</span>}
                <span>if </span>(<span>$k </span><span>== </span><span>'content-type'</span>) {
                    <span>$contenttype </span><span>= </span><span>$v</span><span>;
</span>}
            }
        }

        <span>foreach </span>( <span>$part_lines </span><span>as </span><span>$line_id </span>=> <span>$line </span>) {
            <span>if </span>(<span>$line_id </span><span><span>$line_dat<strong>a_star</strong>t</span>)
                <span>$part_lines </span><span>[</span><span>$line_id</span><span>] = </span><span>''</span><span>;
</span>}

        <span>$part_lines </span><span>= </span>implode ( <span>''</span>, <span>$part_lines </span>)<span>;
</span><span>if </span>(<span>$encoding </span><span>== </span><span>'base64'</span>)
            <span>$part_lines </span><span>= </span>base64_decode ( <span>$part_lines </span>)<span>;
</span><span>elseif </span>(<span>$encoding </span><span>== </span><span>'quoted-printable'</span>)
            <span>$part_lines </span><span>= </span>imap_qprint ( <span>$part_lines </span>)<span>;
</span><span>return </span><span>$part_lines</span><span>;
</span>}

    <span>function </span>read_filedata() {
        <span>$handle </span><span>= </span>fopen ( <span>$this</span><span>-></span>file, <span>'r' </span>)<span>;
</span><span>$this</span><span>-></span>filedata <span>= </span>fread ( <span>$handle</span>, filesize ( <span>$this</span><span>-></span>file ) )<span>;
</span>fclose ( <span>$handle </span>)<span>;
</span>}

    <span>function </span>getrange(<span>&</span><span>$subject</span>, <span>$Beginmark_str </span><span>= </span><span>'{'</span>, <span>$Endmark_str </span><span>= </span><span>'}'</span>, <span>$Start_pos </span><span>= </span><span>0</span>) {
        <span>/*
</span><span>         * $str="sssss { x { xx } {xx{xx } x} x} sssss"; $range=string::getRange($str,'{','}',0); echo $range['range']; //tulem: " x { xx } {xx{xx } x} x" echo $range['behin']; //tulem: 6 echo $range['end']; //tulem: 30 (' ') -- l5pum?¤rgist j?¤rgnev out: array('range'=>$Range,'begin'=>$Begin_firstOccurence_pos,'end'=>$End_sequel_pos) | false v1.1 2004-2006,Uku-Kaarel J5esaar,ukjoesaar@hot.ee,http://www.hot.ee/ukjoesaar,+3725110693
</span><span>         */
</span><span>if </span>(<span>empty </span>( <span>$Beginmark_str </span>))
            <span>$Beginmark_str </span><span>= </span><span>'{'</span><span>;
</span><span>$Beginmark_str_len </span><span>= </span>strlen ( <span>$Beginmark_str </span>)<span>;
</span><span>if </span>(<span>empty </span>( <span>$Endmark_str </span>))
            <span>$Endmark_str </span><span>= </span><span>'}'</span><span>;
</span><span>$Endmark_str_len </span><span>= </span>strlen ( <span>$Endmark_str </span>)<span>;
</span><span>/* $Start_pos_cache = 0; */
</span><span>do </span>{
            <span>/* !algus */
</span><span>if </span>(<span>! </span>is_int ( <span>$Begin_firstOccurence_pos </span>))
                <span>$Start_pos_cache </span><span>= </span><span>$Start_pos</span><span>;
</span><span>/* ?algus-test */
</span><span>$Start_pos_cache </span><span>= @</span>strpos ( <span>$subject</span>, <span>$Beginmark_str</span>, <span>$Start_pos_cache </span>)<span>;
</span><span>/* this is possible start for range */
</span><span>if </span>(is_int ( <span>$Start_pos_cache </span>)) {
                <span>/* skip */
</span><span>$Start_pos_cache </span><span>= </span>(<span>$Start_pos_cache </span><span>+ </span><span>$Beginmark_str_len</span>)<span>;
</span><span>/* test possible range start pos */
</span><span>if </span>(is_int ( <span>$Begin_firstOccurence_pos </span>)) {
                    <span>if </span>(<span>$Start_pos_cache </span><span><span>$range_end_pos</span>)
                        <span>$rangeClean </span><span>= </span><span>0</span><span>;
</span><span>elseif </span>(<span>$Start_pos_cache </span><span>> </span><span>$range_end_pos</span>)
                        <span>$rangeClean </span><span>= </span><span>1</span><span>;
</span>}
                <span>/* here it is */
</span><span>if </span>(<span>! </span>is_int ( <span>$Begin_firstOccurence_pos </span>))
                    <span>$Begin_firstOccurence_pos </span><span>= </span><span>$Start_pos_cache</span><span>;
</span>} <span>/* VIGA NR 0 ALGUST EI OLE */
</span><span>if </span>(<span>! </span>is_int ( <span>$Start_pos_cache </span>)) {
                <span>/* !algus */
</span><span>                /* VIGA NR 1 ALGUSMARKI EI LEITUD : VIIMANE VOIMALIK ALGUS */
</span><span>if </span>(is_int ( <span>$Begin_firstOccurence_pos </span>) <span>and </span>(<span>$Start_pos_cache </span><span><span>$range_end_pos</span>))
                    <span>$rangeClean </span><span>= </span><span>1</span><span>;
</span><span>else
</span><span>                    return false</span><span>;
</span>}
            <span>if </span>(is_int ( <span>$Begin_firstOccurence_pos </span>) <span>and </span>(<span>$rangeClean </span><span>!= </span><span>1</span>)) {
                <span>if </span>(<span>! </span>is_int ( <span>$End_pos_cache </span>))
                    <span>$End_sequel_pos </span><span>= </span><span>$Begin_firstOccurence_pos</span><span>;
</span><span>$End_pos_cache </span><span>= </span>strpos ( <span>$subject</span>, <span>$Endmark_str</span>, <span>$End_sequel_pos </span>)<span>;
</span><span>/* ok */
</span><span>if </span>(is_int ( <span>$End_pos_cache </span>) <span>and </span>(<span>$rangeClean </span><span>!= </span><span>1</span>)) {
                    <span>$range_current_lenght </span><span>= </span>(<span>$End_pos_cache </span><span>- </span><span>$Begin_firstOccurence_pos</span>)<span>;
</span><span>$End_sequel_pos </span><span>= </span>(<span>$End_pos_cache </span><span>+ </span><span>$Endmark_str_len</span>)<span>;
</span><span>$range_end_pos </span><span>= </span><span>$End_pos_cache</span><span>;
</span>}
                <span>/* VIGA NR 2 LOPPU EI LEITUD */
</span><span>if </span>(<span>! </span>is_int ( <span>$End_pos_cache </span>))
                    <span>if </span>(<span>$End_pos_cache </span><span>== </span><span>false</span>)
                        <span>return false</span><span>;
</span>}
        } <span>while </span>( <span>$rangeClean </span><span><span>1 </span>)<span>;
</span><span>if </span>(is_int ( <span>$Begin_firstOccurence_pos </span>) <span>and </span>is_int ( <span>$range_current_lenght </span>))
            <span>$Range </span><span>= </span>substr ( <span>$subject</span>, <span>$Begin_firstOccurence_pos</span>, <span>$range_current_lenght </span>)<span>;
</span><span>else
</span><span>            return false</span><span>;
</span><span>return array </span>(
            <span>'range' </span>=> <span>$Range</span>,
            <span>'begin' </span>=> <span>$Begin_firstOccurence_pos</span>,
            <span>'end' </span>=> <span>$End_sequel_pos
</span>)<span>;
</span>} <span>// end getrange()
</span>} <span>// class
</span><span>/*$filename = './test.mht';
</span><span>if (file_exists ( $filename )) {
</span><span>    if (is_dir ( $filename )) return false;
</span><span>    $filename = strtolower ( $filename );
</span><span>    if (strpos ( $filename, '.mht', 1 ) == FALSE) return false;
</span><span>    $o_mhtml = new mhtparse ();
</span><span>    $o_mhtml->set_file ( $filename );
</span><span>    $o_mhtml->extract ();
</span><span>    $res =  $o_mhtml->get_part_to_file(0);
</span><span>    var_dump($res);
</span><span>}*/</span></span></span></span></span></span>
Salin selepas log masuk

以上就介绍了PHP 处理 mht 文件,包括了a_star方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan