PHP数组与字符串

一、使用循环将用户输入的5个数进行由小到大的排序。

<?php 
	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; } } }*/
		sort($stu);
		echo "排序后的数据如下所示:<br>";
		foreach ($stu as $key => $value) {
   
			echo $value."<br>";
		}
	}
?>

二、由用户输入5个学生的学号,如果有相同的学号则只保留一个,找到前缀为1811的改为1810,最后将所有学号输出,以逗号‘,’为分隔符。

<?php 
	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'])) {
   
		$k = 0;
		$jsj = array();
		$stu = $_POST['stu'];
		//print_r($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, "1811")) {
       //查找包含1811的学号
				$string = str_replace("1811", "1810", $value);
				$jsj[$k] = $string;   //将修改的后的学号赋值给数组
				$k++;
			}
		}
		echo "计算机专业的学号如下:<br>";
		echo implode(",", $jsj);
	}
 ?>

三、使用正则表达式验证表单数据的正确性,表单中包含用户名、密码、出生日期、邮箱。要求用户名为6-12个字符,密码为6-20个数字,出生日期为有效日期,邮箱地址为有效的邮件地址。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>注册页面</title>
	<style>
		.STYLE1 {
   
			font-size: 14px;
			color: red;
		}
		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="注册">&nbsp;&nbsp;&nbsp;
		  		<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 "<script>alert('用户名格式错误!');</script>";
		}
		elseif (!$checkpwd) {
   
			echo "<script>alert('密码格式错误!');</script>";
		}
		elseif (!$checkbirthday) {
   
			echo "<script>alert('出生日期格式错误!');</script>";
		}
		elseif (!$checkEmail) {
   
			echo "<script>alert('E-mail格式错误!');</script>";
		}
		else {
   
			echo "注册成功!";
		}
	}
 ?>

四、接收用户输入的学生学号、姓名、成绩等信息,将接收到的信息存入数组并按照成绩升序排序。之后再以表格形式输出,如果存在学号为1的学生,则输出其姓名与成绩。

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>处理表格数据</title>
 <style>
   table, div, td {
   
   		text-align: center;
   	}
   	table {
   
   		margin: 0 auto;
   	}
   	p {
   
   		font-size: 18px;
   		color: #FF0000;
   	}
   </style>
</head>

<body>
 <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++) {
    
   			echo "<tr><td><input type='text' name='XH[]'></td> <td><input type='text' name='XM[]'></td> <td><input type='text' name='CJ[]'></td> </tr>";
   		}
      ?>
     <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'];
   		
   		/** * print_r($_POST); * Array ( [XH] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) * [XM] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) * [CJ] => Array ( [0] => 78 [1] => 56 [2] => 86 [3] => 49 [4] => 83 ) * [bt_stu] => 提交 ) */
   		foreach ($_POST as $key => $value) {
   
   			$$key=$value;    //使用PHP中的可变变量$$

   			/** * $key=XH; * $value=Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ); * 即$value=[]; * $$key=$value; * 即$$key=[]; * 则:$XH=[]; */
   		}
   		array_multisort($CJ, $XH, $XM);
   		for ($i=0; $i < count($XH); $i++) {
    
   			$sum[$i] = array($XH[$i], $XM[$i], $CJ[$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);
   		
   		foreach ($sum as $key => $value) {
   
   			list($stu_number, $stu_name, $stu_score) = $value;
   			if ($stu_number == '1') {
   
   				echo "<p align=center>";
   				echo $stu_number."的姓名为:".$stu_name;
   				echo "成绩为:".$stu_score;
   				break;
   			}
   		}
   	}
    ?>
</body>

</html>

五、新建一个留言簿,留言簿上有E-mail地址和用户的留言,提取客户的E-mail地址和留言,要求E-mail地址中@符号前不能有点“.”或逗号“,”。将E-mail地址中@符号前的内容作为用户的用户名,并将用户留言中第一人称“我”修改为“本人”。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		p {
   
			font-family: "方正舒体";
			font-size: 18px;
		}
		div {
   
			font-family: "黑体";
			font-size: 18px;
		}
	</style>
</head>
<body>
	<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 "<script>alert('E-mail地址和留言请填写完整!');</script>";
		} 
		else {
   
			$array=explode("@", $Email);
			if (count($array) != 2) {
   
				echo "<script>alert('E-mail地址格式错误!');</script>";
			}
			else {
   
				$username=$array[0];   //取得@符号前的内容
				$netname=$array[1];    //取得@符号后的内容
				//如果username中含有"."或","则报错
				if (strstr($username, ".") or strstr($username, ",")) {
   
					echo "<script>alert('E-mail地址格式错误!');</script>";
				}
				else {
   
					$str1 = htmlspecialchars("<");
					$str2 = htmlspecialchars(">");
					//先将留言都转换为HTML实体,不进行解析。
					$note = htmlspecialchars($note);     //防止XSS攻击
					//将留言中的“我”用“本人”替代
					$newnote = str_replace("我", "本人", $note);
					echo "<div>";
					echo "用户".$str1.$username.$str2."您好!&nbsp";
					echo "您是".$netname."网友!<br>";
					echo "<br>您的留言是:<br>&nbsp;&nbsp;&nbsp;".$newnote."<br>";
					echo "</div>";
				}
			}
		}
	}
	 ?>
</body>
</html>


创作不易,喜欢的话加个关注点个赞,谢谢谢谢