我有一个差不多完成的PHP文档。但是,在将表单通过电子邮件发送给收件人之前,我需要删除所有空字段。下面,我将包含选择所有表单字段以准备发送的代码。我如何对此进行编码以仅选择从用户输入数据的元素?
//Gathering Data
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br />";
$email_body = $email_body . "Address: " . $address . "<br />";
$email_body = $email_body . "City: " . $city . "<br />";
$email_body = $email_body . "State: " . $state . "<br />";
$email_body = $email_body . "Zip: " . $zip . "<br />";
$email_body = $email_body . "Home Phone: " . $hmP . "<br />";
$email_body = $email_body . "Work Phone: " . $wrkP . "<br />";
$email_body = $email_body . "Cell Phone: " . $cellP . "<br />";
$email_body = $email_body . "Fax #: " . $fx . "<br />";
$email_body = $email_body . "Email: " . $email . "<br /><br />";
$email_body = $email_body . "Service Type: " . $service . "<br />";
答案 0 :(得分:1)
正如评论所指出的那样,你可以这样做:
$email_body = "";
$email_body .= !empty($name) ? $email_body . "Name: " . $name . "<br />" : '';
$email_body .= !empty($address) ? $email_body . "Address: " . $address . "<br />" : '';
$email_body .= !empty($city) ? $email_body . "City: " . $city . "<br />" : '';
$email_body .= !empty($state) ? $email_body . "State: " . $state . "<br />" : '';
$email_body .= !empty($zip) ? $email_body . "Zip: " . $zip . "<br />" : '';
$email_body .= !empty($hmP) ? $email_body . "Home Phone: " . $hmP . "<br />" : '';
$email_body .= !empty($wrkP) ? $email_body . "Work Phone: " . $wrkP . "<br />" : '';
$email_body .= !empty($cellP) ? $email_body . "Cell Phone: " . $cellP . "<br />" : '';
$email_body .= !empty($fx) ? $email_body . "Fax #: " . $fx . "<br />" : '';
$email_body .= !empty($email) ? $email_body . "Email: " . $email . "<br /><br />" : '';
$email_body .= !empty($service) ? $email_body . "Service Type: " . $service . "<br />" : '';
这使用三元运算符来确保没有空值附加到电子邮件字符串
你也可以将它包装在一个函数中:
function buildEmail($emailParts) {
$retString = '';
foreach ($emailParts as $part) {
$retString .= !empty($part['value']) ? $part['label'] . ' ' . $part['value'] . '<br />' : '';
}
return $retString;
}
$emailParts = array(
array('label' => 'Name:', 'value' => $name),
array('label' => 'Address:', 'value' => $address)
...
);
$emailBody = buildEmail($emailParts);
答案 1 :(得分:0)
这是我会采取的方法。做一个可以在输出时过滤的设置数组。
// I just isset + !empty, but you can specify is_numeric(),
// preg_replace() etc...
$_settings[] = (isset($name) && !empty($name))? "Name: " . $name:'';
$_settings[] = (isset($city) && !empty($city))? "City: " . $city:'';
$_settings[] = (isset($state) && !empty($state))? "State: " . $state:'';
$_settings[] = (isset($zip) && !empty($zip))? "Zip: " . $zip:'';
$_settings[] = (isset($hmp) && !empty($hmp))? "Home Phone: " . $hmp:'';
$_settings[] = (isset($wrkP) && !empty($wrkP))? "Work Phone: " . $wrkP:'';
$_settings[] = (isset($cellP) && !empty($cellP))? "Cell Phone: " . $cellP:'';
$_settings[] = (isset($fx) && !empty($fx))? "Fax #: " . $fx:'';
$_settings[] = (isset($email) && !empty($email))? "Email: " . $email:'';
$_settings[] = (isset($service) && !empty($service))? "Service Type: " . $service:'';
// Remove empties. You don't really need to remove empties,
// it's a force of habit to filter an array...
$_settings = array_filter($_settings);
// If array is still populated, implode and continue on.
if(!empty($_settings)) {
$email_msg = implode("<br />\r\n",$_settings);
// rest of code here
}
答案 2 :(得分:0)
如果你知道你的变量名,那么你也可以用更复杂的方法来解决你的问题.2。想节省工作......
您可以创建一个包含所有变量名称及其“描述”的数组,例如:
$variables = array(
"name" => "Name: ",
"address" => "Address: ",
"city" => "City: ",
"state" => "State: ",
"zip" => "Zip: ",
"hmP" => "Home Phone: ",
"wrkP" => "Work Phone: ",
"cellP" => "Cell Phone: ",
"fx" => "Fax #: ",
"email" => "Email: ",
"service" => "Service Type: "
);
然后使用每个元素作为变量名循环遍历数组,方法是使用第二个$
询问它是!empty
还是isset
像Fred -ii-那样建议:
foreach($variables as $var => $description)
{
if(isset($$var))
{
$email_body .= $description . $$var . "<br />";
}
}