(1)掌握PHP中处理数组数据的方法; (2)掌握PHP中进行字符串操作的方法;
(3)掌握PHP中正则表达式的使用方法;
实验准备
(1)了解PHP中数组的键和键值的概念; (2)了解操作字符串的各类函数;
(3)了解有关正则表达式的基础知识和Perl兼容正则表达式语法;
一、完成教材P119~121例4.2(文件命名为SY4_4.php)–数组的操作
<!DOCTYPE html>
<meta charset="UTF-8">
<style type="text/css">
table, div, td {
text-align: center;
}
table {
margin: 0 auto;
}
p {
font-size: 18px;
color: #FF0000;
}
</style>
<form action="" name="fr1" method="post">
<table border="1">
<tr>
<td>
<div>学号</div>
</td>
<td>
<div>姓名</div>
</td>
<td>
<div>成绩</div>
</td>
</tr>
<?php
for ($i=0; $i < 5; $i++) {
?>
<tr><td><input type='text' name='XH[]'></td>
<td><input type='text' name='XM[]'></td>
<td><input type='text' name='CJ[]'></td>
</tr>
<?php
}
?>
<tr>
<td colspan="3">
<input type="submit" name="bt_stu" value="提交">
</td>
</tr>
</table>
</form>
<p align="center">注意:学号值不能重复</p><br>
<?php
if (isset($_POST['bt_stu'])) {
$XH = $_POST['XH'];
$XM = $_POST['XM'];
$CJ = $_POST['CJ'];
array_multisort($CJ, $XH, $XM);
for ($i=0; $i < count($XH); $i++) {
$sum[$i] = array($XH[$i], $XM[$i], $CJ[$i]);
}
echo "排序后成绩如下:";
echo "学号 姓名 成绩 ";
foreach ($sum as $value) {
list($stu_number, $stu_name, $stu_score)=$value;
echo "$stu_number $stu_name $stu_score ";
}
echo "
";
reset($sum);
while(list($key,$value)=each($sum)) {
list($stu_number, $stu_name, $stu_score) = $value;
if ($stu_number == '181101') {
echo ""
;
echo $stu_number."的姓名为:".$stu_name;
echo "成绩为:".$stu_score;
break;
}
}
}
?>
二、完成教材P128~129例4.3(文件命名为SY4_5.php)–字符串的操作
<!DOCTYPE html>
<title>留言簿</title>
<style type="text/css">
p {
font-family: "方正舒体";
font-size: 18px;
}
div {
font-family: "黑体";
font-size: 18px;
}
</style>
<form action="" name="f1" method="post">
<p>
您的E-mail地址:<br>
<input type="email" name="Email" size="31"><br>
您的留言:<br>
<textarea name="note" cols="30" rows="10"></textarea>
<br>
<input type="submit" name="bt1" value="提交">
<input type="reset" name="bt2" value="清空">
</p>
</form>
<?php
if (isset($_POST['bt1'])) {
$Email=$_POST['Email']; //接受E-mail地址
$note=$_POST['note']; //接受留言
if (!$Email || !$note) {
echo "";
}
else {
$array=explode("@", $Email);
if (count($array) != 2) {
echo "";
}
else {
$username=$array[0]; //取得@符号前的内容
$netname=$array[1]; //取得@符号后的内容
//如果username中含有"."或","则报错
if (strstr($username, ".") or strstr($username, ",")) {
echo "";
}
else {
$str1 = htmlspecialchars("<");
$str2 = htmlspecialchars(">");
//先将留言都转换为HTML实体,不进行解析。
//$note = htmlspecialchars($note); //防止XSS攻击
//将留言中的“我”用“本人”替代
$newnote = str_replace("我", "本人", $note);
echo "";
echo "用户".$str1.$username.$str2."您好! ";
echo "您是".$netname."网友!
";
echo "
您的留言是:
".$newnote."
";
echo "";
}
}
}
}
?>
三、完成教材P321~323【实验4.3】(文件命名为SY4_3.php)–正则表达式的应用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
<style>
div {
text-align: center;
font-size: 24px;
color: #0000FF;
}
table {
margin: 0 auto;
}
</style>
</head>
<body>
<form action="" name="fr1" method="post">
<div>用户注册</div>
<table border="1" width="480">
<tr>
<td width="80">用户名:</td>
<td><input type="text" name="ID"></td>
<td class="STYLE1">*6~12个字符(数字,字母和下划线)</td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="PWD" size="21"></td>
<td class="STYLE1">*6~20个数字</td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="text" name="BIRTHDAY"></td>
<td class="STYLE1">*有效的日期</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" name="EMAIL"></td>
<td class="STYLE1">*有效的邮件地址</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" name="GO" value="注册">
<input type="reset" name="NO" value="取消">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
if (isset($_POST['GO'])) {
$id = $_POST['ID'];
$pwd = $_POST['PWD'];
$birthday = $_POST['BIRTHDAY'];
$Email = $_POST['EMAIL'];
$checkid = preg_match('#^\w{6,12}$#', $id); //检查是否为6~12个字符
$checkpwd = preg_match('#^\d{6,20}$#', $pwd); //检查是否为6~20个数字
$checkbirthday = preg_match('#^\d{4}-(0?\d|1?[012])-(0?\d|[12]\d|3[01])$#', $birthday); //检查是否是有效日期
$checkEmail = preg_match('#^[a-zA-Z0-9_\-]+@[a-zA-Z0-9\-\.]+$#', $Email); //检查邮件地址的合法性
if (!$checkid) {
echo "";
}
elseif (!$checkpwd) {
echo "";
}
elseif (!$checkbirthday) {
echo "";
}
elseif (!$checkEmail) {
echo "";
}
else {
echo "注册成功!";
}
}
?>
运行结果:
四、完成教材P318~319【实验4.1】(文件命名为SY4_1.php)
<?php
echo "请输入需要排序的数据:
";
echo ";
for ($i=1; $i < 6; $i++) {
echo "";
if ($i<5) {
echo "-";
}
}
echo "";
echo ";
if (isset($_POST['bt'])) {
$temp=0;
$stu=$_POST['stu'];
$num=count($stu);
echo "您输入的数据有:
";
foreach ($stu as $score ) {
echo $score."
";
}
for ($i=0; $i < $num; $i++)
for ($j=$i+1; $j < $num; $j++)
{
if ($stu[$i]>$stu[$j])
{
$temp=$stu[$i];
$stu[$i]=$stu[$j];
$stu[$j]=$temp;
}
}
echo "排序后的数据如下所示:
";
while (list($key,$value) = each($stu)) {
echo $value.'
';
}
}
?>
运行结果:
五、完成教材P320~321【实验4.2】(文件命名为SY4_2.php)
<?php
echo "请输入学号:
";
echo ";
for($i=1;$i<5;$i++)
{
echo "";
if($i<4)
echo "-";
}
echo "";
echo "";
if(isset($_POST['bt']))
{
$k=0;
$jsj=array();
$stu=$_POST['stu'];
for($i=0;$i<count($stu);$i++)
for($j=$i+1;$j<count($stu);$j++)
{
if(strcmp($stu[$i],$stu[$j])==0)
array_splice($stu,$j,1);
}
$str=implode(",",$stu);
echo "所有学生的学号如下:
";
echo $str."
";
foreach($stu as $value)
{
if(strstr($value,"1811"))
{
$string=str_replace("1811","1810",$value);
$jsj[$k]=$string;
$k++;
}
}
echo "计算机专业的学号如下:
";
echo implode(",",$jsj);
}
?>