fopen或file试试吧。按行存入数组后。再用正则换掉该换的字符。最后fwrite进去。

解决方案 »

  1.   

    ciaky(小五) 如何做了,例如:将Word文件中的字符:quit换成good?
      

  2.   

    <?php
    $a = "test.doc";
    $d = fopen($a,"r");
    $c = fread($d,filesize($a));
    $b = str_replace("quit","good",$c);
    fclose($d);
    $f = fopen($a,"w+");
    $e = fwrite($f,$b);
    fclose($f);
    ?>
      

  3.   

    谢谢ciaky(小五) ,这种方法我尝试过的,不行的,因为$c = fread($d,filesize($a));读出来的字符串是二进制或者别的什么的,是找不到quit这样正常的字符的。
      

  4.   

    我刚才测试过了二进制打开的时候是这样的fopen($a,"rb");
      

  5.   

    估计会有字符集问题,
    win下载编码是unicode,是双字节字符
      

  6.   

    fopen($a,"rb"); 换成这种方法之后,无法打开生成的文件的。
      

  7.   

    我的源代码:ciaky(小五)可以试试:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
    <title>Untitled Document</title>
    </head><body>
    <?php
    $strFileData = readfromfile("test.doc");
    echo base64_encode("quit")."<br>";
    echo base64_encode("good")."<br>";
    $strFileData = str_replace(base64_encode("quit"), base64_encode("good"), $strFileData);
    echo $strFileData."<br>";
    if(writetofile ("QDD1.doc", $strFileData))
    echo "ok";
    ?>
    </body>
    </html><?    
    function readfromfile($file_name) 
    { //File Reading 
        if (file_exists($file_name)) 

            //if (Php_VERSION >= "4.3.0") return file_get_contents($file_name); 
            $filenum=fopen($file_name,"r"); 
            $sizeofit=filesize($file_name); 
            if ($sizeofit<=0) 
    return ""; 
            @flock($filenum,LOCK_EX); 
            $file_data=base64_encode(fread($filenum, $sizeofit)); 
            fclose($filenum); 
            return $file_data; 
        } 
    else
    return "Not Read"; 
    } function writetofile ($filename, $data)
    { //File Writing 
        $filenum=@fopen($filename,"w"); 
        if (!$filenum) 

            return false; 
        } 
        flock($filenum,LOCK_EX); 
        $file_data=fwrite($filenum,base64_decode($data)); 
        fclose($filenum); 
        return true; 

    ?>
      

  8.   

    用 new COM("word.application")吧
      

  9.   

    <?
    // create a reference to a new COM component (Word) 
    $word = new COM("word.application") or die("Can't start Word!"); // print the version of Word that's now in use 
    //$word->Documents->Open("test.doc");
    // set the visibility of the application to 0 (false) 
    // to open the application in the forefront, use 1 (true) 
    $word->Visible = 0; // create a new document in Word 
    $word->Documents->Add(); // add text to the new document 
    $word->Selection->TypeText("Testing 1-2-3..."); //save the document in the Windows temp directory 
    $word->Documents[1]->SaveAs("D:/comtest.doc"); // close the connection to the COM component 
    $word->Quit(); // print another message to the screen 
    echo "Check for the file..."; ?>
      

  10.   

    用WORD的话,只有用COM,不然很多东西不能保存的。
    你可以用VB或者其他软件查看一下word.application的参数和函数等。根据函数写吧。不过。。各个版本的application好象都不一样的。以前写过一段,但现在全忘了
      

  11.   

    谢谢关注,word的读写要保留原先的格式的确是一个大问题的。
      

  12.   

    PHP很难实现的,就算实现了,你也要耗费大量的精力,我用C#的COM+实现过一次,所以用高级语言实现
      

  13.   

    php用ODBC驱动可以实现读写word
      

  14.   

    我已经采用了另外一种方法来实现这种功能,参考word文件格式,做了一个一样的html页面,html页面中也可以做出页眉页脚的效果,谢谢大家的帮助,结贴.