字符串操作与正则表达式(代码含有详细的说明)

来源:互联网 发布:qt tcp网络编程 编辑:程序博客网 时间:2024/05/29 17:00

新建一个邮件提交页面 email.html

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>邮件提交</title>
<style type="text/css">
<!--
._title {
 font-size: 24px;
 color: #33F;
}
-->
</style>
</head>

<body>
<form id="form1" name="form1" method="post" action="string.php">
<table width="600" border="1">
  <caption>
    <em class="_title">邮件发送</em>
  </caption>
  <tr>
    <td align="right">发送地址:</td>
    <td><input type="text" name="address" id="address" /></td>
  </tr>
  <tr>
    <td align="right">邮件标题:</td>
    <td>
      <input type="text" name="title" id="title" />
    </td>
  </tr>
  <tr>
    <td align="right" valign="middle">邮件正文:</td>
    <td><textarea name="content" id="content" cols="45" rows="5"></textarea></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="submit" name="button" id="button" value=" 提  交" /></td>
    </tr>
</table>
</form>
</body>
</html>

 

再建一个邮件处理和字符串操作页面:string.php ,代码如下:

 

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>字符串操作和正则表达式</title>
</head>

<body>
<?php
$title = trim($_REQUEST['title']);
$content = trim($_REQUEST['content']);
$address = trim($_REQUEST['address']);
//检杳用户是否在表单中输入了信息,用 empty()函数,没有输入任何信息返回真
if(empty($title) || empty($content) || empty($address))
{
 echo '请在表单里输入内容';
 exit();
}

echo $content.'<br><br>';
//HTML 格式化函数 n12br() htmlspecialchars()
/*$content = n12br($content);
$content = htmlspecialchars($content);
echo $content.'<br><br>'*/

//格式化字符串以便存储
//addslashes()函数将字符串中的特殊字符加上 / 并将处理后的字符串返回,现在新版本的PHP系统自动完成这个函数的功能,不需要用这个函数
/*$content = addslashes($content);
echo $content.'<br><br>';*/
$content = stripslashes($content); //这个函数删除字符串中由 addslashes() 函数添加的 / 并将处理后的字符串返回
echo $content.'<br><br>';

//分割字符串函数 explode(),这个函数根据参数1的字符分割参数2的字符串,并将分割的子字符串以数组返回
$contents = explode(',',$content);
foreach($contents as  $value)
{
 echo $value.'<br>';
}
//连接字符串函数 implode(),这个函数用参数1的字符串把参数2的数组元素连接起来,并将连接后的字符串返回
$contentstr = implode(',',$contents);
echo '<br>'.$contentstr.'<br><br>';

//取得字符串长度
echo '邮件内容的长度是:'.strlen($content);

//区分大小写查找字符串
if(strstr('love',$content))
{
 echo '你很多爱呀<br><br>';
}
//不区分大小写的字符串查找
if(stristr('love',$content))
{
 echo '你的爱太伟大了<br><br>';
}


//替换子字符串,用参数2替换参数1中所有参数3的字符
$content = str_replace($content,'。',',');

//从特定位置替换字符串,用参数2的字符替换从位置1到位置10的字符
// $content = substr_replace($content,'。',1,10);

//使用正则表达式验证邮件地址是否符合规范
if(!eregi('^[a-zA-Z0-9_/-.]+@[a-zA-Z0-9/-]+/.[a-zA-Z0-9/-.]+$',$address))
{
 echo '邮件地址不符合规范<br><br>';
}

/*区分大小写使用正则表达式替换子字符串
string ereg_replace(string pattern,string replacement,string search);
在search中搜索与正则表达式pattern匹配的子字符串,并用replacement替换

eregi_replace()函数除了不区分大小写外,作用与ereg_replace()函数相同
*/

/*使用正则表达式分割字符串
array split(string pattern,string search [,int max]);
这个函数将search分割成符合正则表达式pattern模式的了字符串,并以数组的形式返回,第三个为可选参数,指定进入数组的元素个数
*/
       
       
//邮件发送
if(mail($address,$title,$content))
{
 echo '邮件发送成功';
}
else
{
 echo '邮件发送失败';
}
?>
</body>
</html>