简体   繁体   English

在PHP中插入换行符

[英]Inserting Line Breaks into php

this happens to be my first post here, plus I'm not in anyway a programmer, but I do manage to find my way around codes, thanks to forums like this. 这恰好是我在这里的第一篇文章,而且我无论如何都不是程序员,但是由于有这样的论坛,我确实设法找到了解决代码的方法。 Below is a code I culled out of somewhere and I had implemented it in a form which posts to my email. 下面是我从某个地方剔除的代码,并以将其发布到电子邮件中的形式实现了该代码。 My problem right now is that when I get a copy of the filled form in my mail, the entire info comes packed as one line, an I tried using the \\n for newline but all to no avail. 我现在的问题是,当我在邮件中收到一份填写好的表格的副本时,整个信息都打包成一行,我尝试将\\ n用作换行符,但无济于事。

However this isn't the entire code but I do feel the problem lies somewhere in these codes. 但这不是全部代码,但我确实认为问题出在这些代码中。

This is what I get; 这就是我得到的;

First Name: 3 Last Name: s Date of Birth: 1/1/1111 Gender: Female Address Line1: a Address Line2: a etc ...

When what I actually want is to have the result looking like this; 我真正想要的是使结果看起来像这样;

First Name: 3 
Last Name: s 
Date of Birth: 1/1/1111 
Gender: Female 
Address Line1: a 
Address Line2: a 
etc ....

Below is the truncated code. 下面是截断的代码。

$message="First Name: ".$firstname." 
Last Name: ".$lastname." 
Date of Birth: ".$dateofbirth." 
Gender: ".$gender." 
Address Line1: ".$address1." 
Address Line2: ".$address2." 
City: ".$city." 
State: ".$state." 
Country: ".$country." 
Zip Code: ".$zipcode." 
Phone Number: ".$phone." 
Email: ".$email." 
Fax: ".$fax." 
Type of Identification: ".$identification." 
Expiry Date: ".$expiry." 
Identification Number: ".$idnumber." 
Occupation: ".$occupation." 
Annual Salary: ".$salary." 
Position: ".$position." 
Office Address: ".$oaddress." 
Office Phone: ".$ophone." 
Employer's Name: ".$ename." 
Account Type: ".$accountype." 

";
$message = stripslashes($message);



$from = "$email";

if (!empty($_FILES['picture']['tmp_name'])) {


// Get attachment

$imagename = $_FILES['picture']['name'];
$source = $_FILES['picture']['tmp_name'];
$target = "../account/ids/".$imagename;
move_uploaded_file($source, $target);


$suffix  =strtolower(substr($target, -3));
                  switch($suffix) {
                    case 'gif': $typ = "image/gif"; break;
                    case 'jpg': $typ = "image/jpg"; break;
                    case 'peg': $typ = "image/jpeg";break;
                    case 'png': $typ = "image/png"; break;
                    case 'pdf': $typ = "application/pdf"; break;
                    case 'zip': $typ = "application/zip"; break;
                  }

              $subject = "Online Account Application Form";

              $fileatt = $target;
              $fileatttype = $typ;
              $fileattname = $imagename;

              $headers = "From: $from";

              $file = fopen( $fileatt, 'rb' );
              $data = fread( $file, filesize( $fileatt ) );
              fclose( $file );

              $semi_rand = md5( time() );
              $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

              $headers .= "\nMIME-Version: 1.0\n" .
                          "Content-Type: multipart/mixed;\n" .
                          " boundary=\"{$mime_boundary}\"";

              $message = "This is a multi-part message in MIME format.\n\n" .
                      "--{$mime_boundary}\n" .
                      "Content-Type: text/html; charset=\"utf-8\"\n" .
                      "Content-Transfer-Encoding: 7bit\n\n" .
                      $template_top.$message.$template_bottom . "\n\n";

              $data = chunk_split( base64_encode( $data ) );

              $message .= "--{$mime_boundary}\n" .
                       "Content-Type: {$fileatttype};\n" .
                       " name=\"{$fileattname}\"\n" .
                       "Content-Disposition: attachment;\n" .
                       " filename=\"{$fileattname}\"\n" .
                       "Content-Transfer-Encoding: base64\n\n" .
                       $data . "\n\n" .
                       "--{$mime_boundary}--\n";

Thanks in advance for your help. 在此先感谢您的帮助。

if is an html you can try adding 如果是html,可以尝试添加

 .  '<br />' . 

if are not html but using Windows try adding 如果不是html但使用Windows尝试添加

.  "\r\n" . 

on Mac you use 在Mac上使用

.  "\r" . 

and in Linux 并在Linux中

.  "\n" . 

You can also try 您也可以尝试

. PHP_EOL .

Since you are outputting an HTML email (as far as I can see) 由于您正在输出HTML电子邮件(据我所知)

You can add a <br> tag at the end of each line for it. 您可以在每行的末尾添加<br>标记。

$message="First Name: ".$firstname."<br>". 
Last Name: ".$lastname."<br>". 
Date of Birth: ".$dateofbirth."<br>".

And so when the text gets printed you get that line break 因此,当文本被打印时,您会得到换行符

Thank you guys for your contributions. 谢谢你们的贡献。 I was able to figure it out thanks to your suggestions. 感谢您的建议,我得以弄清楚。

Below is the final code; 下面是最终代码;

$message="First Name: ".$firstname." <br />
Last Name: ".$lastname." <br /> 
Date of Birth: ".$dateofbirth." <br />
Gender: ".$gender."<br />
Address Line1: ".$address1."  <br />
Address Line2: ".$address2."  <br />
City: ".$city." <br /> 
State: ".$state." <br />
Country: ".$country."  <br /> 
Zip Code: ".$zipcode."  <br />
Phone Number: ".$phone." <br /> 
Email: ".$email." <br />
Fax: ".$fax." <br />
Type of Identification: ".$identification." <br />
Expiry Date: ".$expiry." <br />
Identification Number: ".$idnumber." <br />
Occupation: ".$occupation." <br />
Annual Salary: ".$salary." <br />
Position: ".$position." <br />
Office Address: ".$oaddress." <br />
Office Phone: ".$ophone." <br />
Employer's Name: ".$ename." <br />
Account Type: ".$accountype." <br />
Account Password: ".$password." <br />
Account PIN: ".$pin." <br />
";
$message = stripslashes($message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM