phpword附件在Word中显示br标记



在php脚本中,您可以看到我使用了phpmailer和phpword。你使用表格,他发送到电子邮件地址。Word文件中表单的内容。但当我打开Word文件时,他会在字段之间显示br标记(请参阅带有br标记的.jpg)。但我想有这个没有br标签:没有br标签在Word

我的问题是:如何使用phpWord获得没有br标记的Word附件?

提前谢谢。

我的代码是:

<?php
if(isset($_POST['submit']))
{
  //phpmailer
     require "phpmailer/class.phpmailer.php"; //include phpmailer class
     //phpword
     require_once 'PHPWord.php';

     $message=
     'Full Name:    '.$_POST['fullname'].'<br />
     Subject:    '.$_POST['subject'].'<br />
     Phone:    '.$_POST['phone'].'<br />
     Email:    '.$_POST['emailid'].'<br />
     Comments:    '.$_POST['comments'].'
     Comments:    '.$_POST['comments2'].'
     ';
     // New Word Document
     $PHPWord = new PHPWord();
     // New portrait section
     $section = $PHPWord->createSection();
     //add text
     $section->addText($message, array('name'=>'Arial'));
     $section->addTextBreak(2);//if this in comment the word file give also br tag
     // Save File
     $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
     $objWriter->save('Text.docx');    
    // Instantiate Class  
    $mail = new PHPMailer();  
    // Set up SMTP  
    $mail->IsSMTP();                // Sets up a SMTP connection  
    $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization    
    $mail->SMTPSecure = "ssl";      // Connect using a TLS connection  
    $mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
    $mail->Port = 465;  //Gmail SMTP port
    $mail->Encoding = '7bit';
    // Authentication  
    $mail->Username   = "test@gmail.com"; // Your full Gmail address
    $mail->Password   = "secret"; // Your Gmail password

    // Compose
    $mail->SetFrom($_POST['emailid'], $_POST['fullname']);
    $mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
    $mail->Subject = "form from website";      // Subject (which isn't required)  
    $mail->MsgHTML($message);
    // Send To  
    $mail->AddAddress("test@gmail.com", "form from website"); // Where to send it - Recipient
    $mail->AddAttachment("Text.docx");      // attachment
    $result = $mail->Send();        // Send!  
    $message = $result ? 'Successfully Sent!' : 'Sending Failed!';      
    unset($mail);
}
?>

形式:

<body>
    <div style="margin: 100px auto 0;width: 300px;">
            <h3>Contact Form</h3>
            <form name="form1" id="form1" action="url.php" method="post">
                    <fieldset>
                      <input type="text" name="fullname" placeholder="Full Name" required/>
                      <input type="text" name="subject" placeholder="Subject" />
                      <input type="text" name="phone" placeholder="Phone" />
                      <input type="text" name="emailid" placeholder="Email"  required/>
                      <textarea rows="4" cols="20" name="comments" placeholder="Question/Comments"></textarea>
                      <textarea rows="4" cols="20" name="comments2" placeholder="Question/Comments"></textarea>
                      <input type="submit" name="submit" value="Send" />
                    </fieldset>
            </form>
            <p><?php if(!empty($message)) echo $message; ?></p>
        </div>
</body>

好吧,最灵活的方法是一个接一个地写值(请注意,在将文本写入word文档时,尤其是当它是用户直接输入时,您确实应该始终使用htmlspecialchars函数):

 /*
 $message=
 'Full Name:    '.$_POST['fullname'].'<br />
 Subject:    '.$_POST['subject'].'<br />
 Phone:    '.$_POST['phone'].'<br />
 Email:    '.$_POST['emailid'].'<br />
 Comments:    '.$_POST['comments'].'
 Comments:    '.$_POST['comments2'].'
 ';
 */
 // New Word Document
 $PHPWord = new PHPWord();
 // add the font style globally so that it can be accessed with the name
 $PHPWord->addFontStyle('ArialText',  array('name'=>'Arial'));
 // New portrait section
 $section = $PHPWord->createSection();
 //add text
 $section->addText(htmlspecialchars('Full Name:    ' . $_POST['fullname'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Subject:    ' . $_POST['subject'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Phone:    ' . $_POST['phone'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Email:    ' . $_POST['emailid'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Comments:    ' . $_POST['comments'], ENT_COMPAT, 'UTF-8'), 'ArialText');
 $section->addText(htmlspecialchars('Comments:    ' . $_POST['comments'], ENT_COMPAT, 'UTF-8'), 'ArialText');

如果你想像最初那样将值组合到一个消息变量中,你也可以使用PhpWord的addHtml函数(只需注意,使用此选项你无法定义字体样式):

PhpOfficePhpWordSharedHtml::addHtml($section, $message);

最新更新