2018.6.16 PHP小实验
Posted Legend
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018.6.16 PHP小实验相关的知识,希望对你有一定的参考价值。
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");
}
?>
实验三
<?php
/**
- Created by PhpStorm.
- User: qichunlin
- Date: 2018/5/17
Time: 下午4:36
*/
if (isset($_POST[‘GO‘])) {
$id = $_POST[‘ID‘];
$pwd = $_POST[‘PWD‘];
$birthday = $_POST[‘BIRTHDAY‘];
$Email = $_POST[‘EMAIL‘];
//检查是否为6~12个字符
$checkId = preg_match(‘/^w{6,12}$/‘, $id);
//检查是否为6~20个数字
$checkPwd = preg_match(‘/^d{6,20}$/‘, $pwd);
//检查是否是有效日期
$checkBirthday = preg_match(‘/^d{4}-(0?d|1?[012])-(0?d|3[01])$/‘, $birthday);
//检查Email地址的合法性
$checkEmail = preg_match(‘/^[a-zA-Z0-9_-][email protected][a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/‘, $Email);if (!$checkId)
echo "";
else if (!$checkPwd)
echo "";
else if (!$checkBirthday)
echo "";
else if (!$checkEmail)
echo "";
else
echo "注册成功!";
}
?>
```
实验四
<!DOCTYPE html>
<!--HTML5表单-->
<style type="text/css">
table, div, td {
text-align: center;
}
table {
margin: 0 auto;
}
p {
font-size: 18px;
color: #FF0000;
}
</style>
<form name=fr1 method=post>
<table border=1>
<tr>
<td>
<div>学号</div>
</td>
<td>
<div>姓名</div>
</td>
<td>
<div>成绩</div>
</td>
</tr>
<?php
/**
* Created by PhpStorm.
* User: qichunlin
* Date: 2018/5/20
* Time: 下午2:56
*/
for ($i = 1; $i < 5; $i++) {
?>
<tr>
<td><input type=text name="XH[]"></td>
<td><input typet=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>
<?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]);
echo "<div>排序后的成绩表如下:</div>";
echo "<table border=2><tr><td>学号</td><td>姓名</td><td>成绩</td></tr>";
foreach ($sum as $value) {
list($stu_number, $stu_name, $stu_score) = $value;
echo "<tr><td>$stu_number</td><td>$stu_name</td><td>$stu_score</td></tr>";
}
echo "</table><br/>";
reset($sum);
while (list($key, $value) = each($sum)) {
list($stu_number, $stu_name, $stu_score) = $value;
if ($stu_number == "3") {
echo "<p align=center>";
echo $stu_number . "的姓名为:" . $stu_name . ",";
echo "成绩为:" . $stu_score;
break;
}
}
}
?>
实验五
<!DOCTYPE html>
<!--HTMl5表单-->
<style type="text/css">
p {
font-family: "方正舒体";
text-align: center;
}
div {
font-family: "黑体";
text-align: center;
}
</style>
<form name="f1" action="" method="post">
<p>
您的Email地址:<br><input type="text" name="Email" size="31"><br>
您的留言:<br><textarea name="note" rows="10" cols="30"></textarea>
<br><input type="submit" name="bt1" value="提交"><input type="reset" name="bt2" value="清空">
</p>
</form>
<?php
/**
* Created by PhpStorm.
* User: qichunlin
* Date: 2018/5/20
* Time: 下午6:37
*/
if (isset($_POST[‘bt1‘])) {
//接收Email地址
$Email = $_POST[‘Email‘];
$note = $_POST[‘note‘];
//判断是否取得值
if (!$Email || !$note)
echo "<script>alert(‘Eamil地址和留言请填写完整!‘)</script>";
else {
//分割Email地址
$array = explode("@", $Email);
//如果有两个@符号则报错
if (count($array) != 2)
echo "<script>alert(‘Email地址格式错误!‘)</script>";
else {
//取得@前面的内容
$username = $array[0];
//取得@后面的内容
$netname = $array[1];
//如果username中含有"."或","就会报错
if (strstr($username, ".") or strstr($username, ","))
echo "<script>alert(‘Email地址格式错误!‘)</script>";
else {
$str1 = htmlspecialchars("<");//输出符号"<"
$str2 = htmlspecialchars(">");//输出符号">"
$newnote = str_replace("我", "本人", $note);
//xss攻击 转化成HTML实体形式就能解决
$newnote=htmlspecialchars($newnote);
echo "<div>";
echo "用户" . $str1 . $username . $str2 . "您好! ";
echo "您是" . $username . "网友!<br>";
echo "<br>您的留言是:<br> " . $newnote . "<br>";
echo "</div>";
}
}
}
}
?>
以上是关于2018.6.16 PHP小实验的主要内容,如果未能解决你的问题,请参考以下文章