html标签在Microsoft Word文档中不起作用

recently i am facing a problem while working with "creating a word document file using in php".

in my website, there is a form. which includes 10 fields such as "name", "phone", "address" etc.

when a user fills up these fields and submits the form, an email is sent to the administrator including the form and corresponding field values saying "mr xxx has submitted this form recently".

i am using file handler for this. but when i am using html tags to make the file look better in word doc, html is not working there, rather it is printed in that doc file as a text.

here is my code--->

$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);

here $data is the variable which consist of the mail body. and i am using str_replace to store the values of the fields of that form.

but the document is showing as follows--->

<p><label>name</label>:<b>arnas sinha</b><br/>
<label>phone</label>:<b>9878790989</b><br/>
 <label>address</label>:<b>india</b><br/>

etc.

how to use the html format for creating word doc file?

You can resolve this using http headers :

<?php
    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment;Filename=document_name.doc");

    echo "<html>";
    echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
    echo "<body>";
    echo "<b>My first document</b>";
    echo "</body>";
    echo "</html>";
?>

Add the above headers to your file and this will do the trick.

Another way would be via COM objects. But the above one should be enough.