如何在2个chunk_split字符串之间添加断行

时间:2017-04-20 06:41:11

标签: php chunks

我正在尝试使用php向附件发送2个字符串。一切都很好,除了我得到2个字符串,它们之间没有空格。我想要新线上的第二个字符串。如何实现这一目标。任何帮助将不胜感激。谢谢。

我的代码:

 //Capture POST data from HTML form and Sanitize them, 
    $sender_name    = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); //sender name
    $reply_to_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
    $fname        = filter_var($_POST["fname"], FILTER_SANITIZE_STRING); 
    $lname        = filter_var($_POST["lname"], FILTER_SANITIZE_STRING);



//plain text 
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
        $body .= chunk_split(base64_encode($fname)); 
        $body .= chunk_split(base64_encode($lname));   

我想在fname和lname之间换行。谢谢。

3 个答案:

答案 0 :(得分:0)

如果您要从浏览器查看此输出,则必须使用<br>

试试这个,

$fname="shashank";
$lname="shah";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
$body .= chunk_split(base64_encode($fname)); 
$body .= "<br>";
$body .= chunk_split(base64_encode($lname));  
echo $body;

另一种方式是

$body .= nl2br(chunk_split(base64_encode($fname))."\n".chunk_split(base64_encode($lname)));

答案 1 :(得分:0)

或者您可以尝试使用PHP_EOL

e.g。 $body .= chunk_split(base64_encode($fname)).PHP_EOL;

答案 2 :(得分:0)

经过大量搜索后,我发现只需在每个var工作后附加键盘返回字符串。不是最优雅的解决方案,但是做到了。

$fname = filter_var($_POST["fname"], FILTER_SANITIZE_STRING) .'
';
$lname = filter_var($_POST["lname"], FILTER_SANITIZE_STRING);