文章目录
字符串操作与正则表达式
使用PHP的字符串函数来格式化和操作文本;使用字符串函数或正则表达式来搜索(替换)单词、短语或字符串的其他模式。
创建一个示例应用:智能表单邮件
<!-- feedback.html -->
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Customer Feedback</title>
</head>
<body>
<h1>Customer Feedback</h1>
<p>Please tell us what you think.</p>
<form action="processfeedback.php" method="post">
<p><strong>Your name:</strong><br/>
<input type="text" name="name" size="40" /></p>
<p><strong>Your email address:</strong><br/>
<input type="text" name="email" size="40" /></p>
<p><strong>Your feedback:</strong><br/>
<textarea name="feedback" rows="8" cols="40">
</textarea></p>
<p><input type="submit" value="Send Feedback" /></p>
</form>
</body>
</html>
<!-- processfeedback.php -->
<?php
//create short variable names
$name = $_POST['name'];
$email = $_POST['email'];
$feedback = $_POST['feedback'];
//set up some static information
$toaddress = "feedback@example.com";
$subject = "Feedback from web site";
$mailcontent = "Customer name: ".filter_var($name)."\n".
"Customer email: ".$email."\n".
"Customer comments:\n".$feedback."\n";
$fromaddress = "From: webserver@example.com";
//invoke mail() function to send email
mail($toaddress, $subject, $mailcontent, $fromaddress);
?>
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Feedback Submitted</title>
</head>
<body>
<h1>Feedback submitted</h1>
<p>Your feedback has been sent.</p>
</body>
</html>
mail()函数:用来发送电子邮件,要使用该函数,必须在PHP安装配置中设置执行邮件发送的程序。
bool email(string to, string subject, string message,
string [additional_headers [, string additional_parameters]]);
该函数的前三个参数是必需的,分别代表发送邮件的收件地址、主题行和消息内容。
第四个参数可以用来发送任何额外的、有效的邮件header。如果需要附加多个邮件header,只要用换行符(\n\r)在字符串中将它们分开即可。
$aadditional_headers = "From: webserver@example.com\r\n "."Reply-To