PHP实验
实验一
<?php
/**
* Created by PhpStorm.
* User: qichunlin
* Date: 2018/5/17
* Time: 下午5:35
*/
echo "请输入要排序的数字:<br/>";
echo "<form method=post>";
//循环生成文本框
for ($i = 1; $i < 6; $i++) {
//文本框的名字是数组名
echo "<input type='text' name='stu[]' size='5'>";
if ($i < 5) {
echo "-";
}
}
echo "<input type='submit' name='bt' value='提交'>";
echo "</form>";
if (isset($_POST['bt'])) {
$temp = 0;
$stu = $_POST['stu'];
$num = count($stu);
echo "您输入的数据有<br/>";
foreach ($stu as $score) {
echo $score . "<br>";
}
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 "排序后的数据如下所示:<br/>";
//使用while循环遍历数组
while (list($key, $value) = each($stu)) {
//输出排序后的数组
echo $value . "<br/>";
}
}
?>
实验二
<?php
/**
* Created by PhpStorm.
* User: qichunlin
* Date: 2018/5/17
* Time: 下午6:10
*/
echo "请输入学号:<br/>";
echo "<form method='post'>";
for ($i = 1; $i < 6; $i++) {
//文本框的名字就是数组名
echo "<input type='text' name='stu[]' size='6'>";
if ($i < 5)
echo "-";
}
echo "<input type='submit' name='bt' value='提交'>";
echo "</form>";
if (isset($_POST['bt'])) {
//echo "123";
$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 "所有的学生学号如下:</br>";
echo $str . "<br>";
foreach ($stu as $value) {
if (strstr($value, "0811")) {
$string = str_replace("08121", "08110", $value);
$jsj[$k] = $string;
$k++;
}
}
echo "计算机专业的学号如下:</br>";
echo implode(",", "$jsj");
}
?>
实验三
注意,学号值不能重复
排序后的成绩表如下:"; echo ""; foreach ($sum as $value) { list($stu_number, $stu_name, $stu_score) = $value; echo ""; } echo "学号 | 姓名 | 成绩 |
$stu_number | $stu_name | $stu_score |
"; reset($sum); while (list($key, $value) = each($sum)) { list($stu_number, $stu_name, $stu_score) = $value; if ($stu_number == "3") { echo "
"; echo $stu_number . "的姓名为:" . $stu_name . ","; echo "成绩为:" . $stu_score; break; } } } ?>
###实验五
alert('Eamil地址和留言请填写完整!')";
else {
//分割Email地址
$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(">");//输出符号">"
$newnote = str_replace("我", "本人", $note);
//xss攻击 转化成HTML实体形式就能解决
$newnote=htmlspecialchars($newnote);
echo "";
echo "用户" . $str1 . $username . $str2 . "您好! ";
echo "您是" . $username . "网友!
"; echo "
您的留言是:
" . $newnote . "
"; echo "
";
}
}
}
}
?>
"; echo "
您的留言是:
" . $newnote . "
"; echo "