最近我在使用“在 php 中创建 word 文档文件”时遇到了一个问题。
在我的网站上,有一个表格。其中包括“姓名”、“电话”、“地址”等10个字段。
当用户填写这些字段并提交表单时,会向管理员发送一封电子邮件,其中包括表单和相应的字段值,内容为“xxx 先生最近提交了此表单”。
我正在为此使用文件处理程序。但是当我使用 html 标签使文件在 word doc 中看起来更好时,html 在那里不起作用,而是作为文本打印在该 doc 文件中。
这是我的代码--->
$fh = fopen(JPATH_BASE.'/files/form_'.$user->id.'_'.$form_id.'_'.$submit_id.'.doc',"w+");
//print_r($post); exit;
//ob_start();
//$data = ob_get_clean();
//$data = '';
$data = $emailreceipttext;
foreach($fieldsInArray as $fields)
{
foreach($post as $key=>$p)
{
if($key==$fields->name)
{
if(is_array($p))
{
$p = implode(",",$p);
}
$data = str_replace("{".$fields->name."}", $p, $data);
}
}
}
//fwrite($fh, $data);
//print_r($data);exit;
//}
/*print_r($data);
exit;*/
fwrite($fh, $data);
$file = 'form_'.$user->id.'_'.$form_id.'_'.$submit_id.'.doc';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
fclose($fh);
//echo hi; print_r($data); exit;
$this->send_mail($data,$submit_id);
这里 $data 是包含邮件正文的变量。我正在使用 str_replace 来存储该表单字段的值。
但文件显示如下--->
<p><label>name</label>:<b>arnas sinha</b><br/>
<label>phone</label>:<b>9878790989</b><br/>
<label>address</label>:<b>india</b><br/>
等等
如何使用 html 格式创建 word doc 文件?