获取word文档的内容,替换字符串,并使用codeigniter php将数据作为word文档发送到下载

获取word文档的内容,替换字符串,并使用codeigniter php将数据作为word文档发送到下载,php,codeigniter,Php,Codeigniter,在此之后,将下载文件,其中包含损坏的数据和一些特殊字符,如�� 以此类推,就像在file\u get\u contents之后回显$contents一样。 如果未将用户名传递给sample(),则下载可以很好地处理实际内容。请尝试使用 $contents=str_replace(“$[Client name]”,$username,$contents) 你可以试试这个,因为它对我有用 public function sample($username = false) { $this-&g

在此之后,将下载文件,其中包含损坏的数据和一些特殊字符,如�� 以此类推,就像在file\u get\u contents之后回显$contents一样。 如果未将用户名传递给sample(),则下载可以很好地处理实际内容。

请尝试使用


$contents=str_replace(“$[Client name]”,$username,$contents)

你可以试试这个,因为它对我有用

public function sample($username = false) {
    $this->load->helper('download');
    $filename = base_url() . "resources/Sample-Letter.doc";

    $contents = file_get_contents($filename); // Read the file's contents

    if ($username) {
        $contents = str_replace('string_to_find', $username, $contents);
        // replace the string
    }
    $name = 'Sample-Letter.doc';

    force_download($name, $contents);
}
那就试试这个

$contents = str_replace('[Client name]', $username, $contents);    
$contents = preg_replace('/[^A-Za-z0-9\-]/', '', $contents);//it will remove all special characters from a string

[客户端名称]-这只是DocumentTested一词中的一个字符串。正在下载文件,但没有任何更改。此操作刚刚删除了特殊字符��. 但也有一些不必要的字符,它改变了数据中的所有字符、字体大小和空格。在同一期之前,它已按上述方式下载了一页的文件。顺便说一句,我想这不是preg_替换的问题,而是我下载的方式。在替换Word文档中的字符串后,它将被完全更改。如果我不更改它下载的数据。
'/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s' instead of '/[^A-Za-z0-9\-]/'