我有一個php表單,當發佈時發送確認電子郵件。表格有幾個下拉列表,當填充時顯示零件的數量和名稱。我有一切工作正常,在我的$ usermessage中,我列出了所有對應於下拉列表的變量。我只想顯示已經實現的下拉列表和已填充的名稱。我無法弄清楚如何在變量未被填充時刪除多餘的空格。如何刪除php電子郵件中的空行
我的確認郵件看起來是這樣的,當我有前兩個下拉菜單和一個第八填充:
用戶信息
名稱
李四
電子郵件地址
[email protected]
地址
12345 street
US
鎮州郵政編碼
電話號碼
數量
編號
CH171181
CH171182
CH171363
正如你所看到的,有空間補償無人居住變量的巨大差距。
這裏是我的代碼:
$email = $_POST["email"];
$myemail ="$email";
$CH171181 = $_POST["CH171181"];
$CH171182 = $_POST["CH171182"];
$CH171183 = $_POST["CH171183"];
$CH171184 = $_POST["CH171184"];
$CH171360 = $_POST["CH171360"];
$CH171361 = $_POST["CH171361"];
$CH171362 = $_POST["CH171362"];
$CH171363 = $_POST["CH171363"];
$CH171364 = $_POST["CH171364"];
$CH171404 = $_POST["CH171404"];
$CH171408 = $_POST["CH171408"];
if (empty($CH171181)) {$var1 = '';} else {$var1 = 'CH171181';}
if (empty($CH171182)) {$var2 = '';} else {$var2 = 'CH171182';}
if (empty($CH171183)) {$var3 = '';} else {$var3 = 'CH171183';}
if (empty($CH171184)) {$var4 = '';} else {$var4 = 'CH171184';}
if (empty($CH171360)) {$var5 = '';} else {$var5 = 'CH171360';}
if (empty($CH171361)) {$var6 = '';} else {$var6 = 'CH171361';}
if (empty($CH171362)) {$var7 = '';} else {$var7 = 'CH171362';}
if (empty($CH171363)) {$var8 = '';} else {$var8 = 'CH171363';}
if (empty($CH171364)) {$var9 = '';} else {$var9 = 'CH171364';}
if (empty($CH171404)) {$var10 = '';} else {$var10 = 'CH171404';}
if (empty($CH171408)) {$var11 = '';} else {$var11 = 'CH171408';}
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$address = $_POST["address"];
$address2 = $_POST["address2"];
$country = $_POST["country"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
$phone = $_POST["phone"];
$user = "$myemail";
$usersubject = "Subject";
$userheaders = "From: [email protected]port.com\n";
$usermessage = "
Users Information
Name
$first_name $last_name
Email address
$email
Address
$address
$address2
$country
$city $state $zip
Phone Number
$phone
Qty
Item #
$CH171181
$var1
$CH171182
$var2
$CH171183
$var3
$CH171184
$var4
$CH171360
$var5
$CH171361
$var6
$CH171362
$var7
$CH171363
$var8
$CH171364
$var9
$CH171404
$var10
$CH171408
$var11
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($user,$usersubject,$usermessage,$userheaders,$headers);
}
也可以使用的preg_replace下降空白 – clearshot66
告訴我們,從這些變量構建實際的電子郵件內容的代碼 –
訪問此問題,您會發現回答[這裏](https://stackoverflow.com/questions/709669/how-do-i-remove-blank-lines-from-text-in-php) –