php实验报告一Word格式文档下载.docx
《php实验报告一Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《php实验报告一Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。
![php实验报告一Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2023-2/4/103fcbf6-92d3-4531-9c8a-0090acc8d514/103fcbf6-92d3-4531-9c8a-0090acc8d5141.gif)
一、实验运行环境
Windows2000+php5、0+apache2、0
二、实验目得
1、掌握正则表达式得使用方法
2、掌握php获取html表单数据得方法
3、掌握php中得文件处理得使用方法
4、掌握类得创建方法及应用
三、实验要求
1、某表单及验证表单得程序如3-1文件夹,请运行该程序并理解check、js文件中得正则表达式。
〈?
php
$='hello/1、txt’;
$somecontent =”我先被写入得!
\r\n"
;
$somecontent1= ”我后被写入得!
\r\n”;
//首先我们要确定文件存在并且可写
if(is_writable($)){
//在这个例子里,将使用只写模式打开$
//文件指针将会在文件得开头,如果原来文件中有其她数据,则会被删除
if(!
$handle= fopen($,”w"
)){
print "
不能打开文件$";
exit;
}//将$somecontent写入到我们打开得文件中。
if (!
fwrite($handle,$somecontent)){
print"不能写入到文件$"
;
exit;
}
print”成功地将\”$somecontent \"写入到文件$〈br〉”;
fclose($handle);
ﻩ$handle= fopen($,”w");
//再次打开
ﻩ//继续打开文件写入另外一个字符串,原来写入得数据则被清除
fwrite($handle, $somecontent1);
fclose($handle);
print"
成功地将\”$somecontent1\"
写入到文件$"
}else {
print ”文件$ 不可写”;
}?
〉
2、使用正则表达式编写程序验证电话号码或url地址。
3、字符串:
$text= "华北科技学院电子商务B11-1、2、3班电子商务应用开发技术课程”;
请编写程序截取该字符串得前16个字。
4、某字符串:
$pizza= ”a1 a2a3a4a5 a6”;
//通过空格分隔请编写程序以空格作为分隔符分隔该字符串并分别输出。
5、某表单如下,请编写获取表单得程序:
6、利用ajax或JQuery实现表单验证.运行3—6文件夹中得程序并理解ajax或JQuery实现表单验证得方法。
7、编写程序利用copy函数实现文件得上传
<
html>
〈head>
meta—equiv=”Content—Type"content="
text/html;
charset=gb2312”>
<title〉文件上传〈/title>
〈/head>
body>
〈?
php
if($_POST['post_flag’]==1)
{
$a=copy($myfile,"upload_file/”、$my);
}
?
〈formenctype=”multipart/form-data”
action=”index2、php”method="
post”>
您得姓名:
〈inputtype=textname=user>
〈br>
文件名称:
〈inputname="
myfile"
type="
file”>
inputtype=”hidden” name="
post_flag"
value="
1”〉
inputtype="
submit”value="
提交”>
/form〉
?
ﻩ$myfile= $_FILES['
myfile'
][’tmp_name’];
if($my"
"
)
{
if(copy($myfile,$_FILES[’myfile'
]['
name’]))
{
echo”上传成功"
}else
{
echo ”上传失败";
}
unlink($myfile);
} else
{
echo"
没有可上传得文件";
8、利用php中得文件处理技术编写程序如下:
请运行该程序并理解。
php
$= '
hello/1、txt’;
$somecontent="
我先被写入得!
$somecontent1=”我后被写入得!
// 首先我们要确定文件存在并且可写
if(is_writable($)) {
// 在这个例子里,将使用只写模式打开$
//文件指针将会在文件得开头,如果原来文件中有其她数据,则会被删除
if (!
$handle=fopen ($,”w”)){
print ”不能打开文件$"
exit;
}//将$somecontent写入到我们打开得文件中.
if (!
fwrite($handle, $somecontent)){
print”不能写入到文件$"
exit;
print”成功地将\"
$somecontent\”写入到文件$<
br〉";
fclose($handle);
ﻩ$handle = fopen($, ”w");
//再次打开
//继续打开文件写入另外一个字符串,原来写入得数据则被清除
fwrite($handle, $somecontent1);
fclose($handle);
ﻩprint"
成功地将\"
$somecontent1\"
} else{
print"文件$不可写"
}?
>
9、请编写程序实现动态页面转化为静态页面。
10、使用类编写程序如下:
classFoo//定义基类
{
ﻩ//使用static关键字定义变量
publicstatic$my_static='
foo'
publicfunctionstaticValue(){
returnself:
:
$my_static;
}
}
class Bar extendsFoo//创建基类
public functionfooStatic(){
returnparent:
:
$my_static;
//返回基类得变量$my_static
}
}
//打印基类得$my_static变量
//虽然没有创建类实例,但就是可以直接访问static变量
print'
Foo:
$my_static结果为’、Foo:
$my_static、”<
br>
”;
$foo=newFoo();
//创建基类实例
print$foo—>staticValue()、 "〈br>”;
//通过方法放回static变量
print$foo-〉my_static、 ”<
br>
//$my_static 为未定义得属性,所以为空
//通过子类访问$my_static
printBar:
$my_static、"<
$bar=newBar();
print$bar->
fooStatic();
//返回父类得static变量?
〉
11、利用类编写程序实现文件上传.见文件夹3-10。
php
$Submit= $_FILES['
Submit’]['
tmp_name’];
if($Submit!
='
'
$['
file’] =$file;
$['
name']=$;
$[’size'
] = $;
type’] =$;
$=array(’gif'
,'
jpg’,’jpge’,’png'
,’txt'
);
$savepath="C:
\AppServ\myphp\"
$maxsize=0;
$overwrite =0;
$upload=newupload($,$,$savepath, $, $overwrite, $maxsize);
if(!
$upload—〉run())
echo"
上传失败"、$upload->
errmsg();
}}?
12、php显示中文时,经常出现乱码,请编写一个编码转换类,从而实现编码得自动转换.
class changecode{
ﻩprivate$str;
private $input;
private$output;
function__construct($input='’,$output='
gb2312'
){
$this—〉input=$input;
$this—〉output = $output;
ﻩprotected functionchgcode($str){
if($this->
input!
='’){
ﻩ$this—>
str =iconv($input,$output,$str);
ﻩ}else{
ﻩ$this-〉str=mb_convert_encoding($str,$output);
ﻩ}
ﻩfunctiongetStr($str){
$this—>chgcode($str);
return$this—〉str;
ﻩ}
functionsetInput($input){
ﻩ$this->
input=$input;
function setOutput($output){
ﻩ$this—>
output=$output;
functiongetInput(){
ﻩﻩreturn$this—>
input;
function getOutput(){
ﻩreturn $this—〉output;
13、做web开发时,需要对各种情况作出处理,并输出相应得处理。
请编写一个输出类,根据不同得情况,输出不同得处理结果。
tableborder=”0”cellpadding=”0”cellspacing=”0"
>
〈formmethod=”post"
action=”111、php ”>
ﻩ<
tr>
ﻩ〈td>
请输入用户名称:
/td>
ﻩ<td>
〈inputname="
name"
type="
text" size="
20” value=”"
/>
/td>
〈/tr〉
<
tr〉
〈td〉〈inputtype="
submit”value="
确认"/>
〈/td>
<
/tr〉
</form>
/table>
include_once"
function、php"
if($_POST[’name’]!
= ”范春喜"
){
ﻩﻩ$tm—> refurbish('用户名错误'
);
ﻩ}else{
$tm->skipachange(’登陆成功’,'
main、php’);
function、php
php
classTakeMsg{
ﻩfunction __construct(){
ﻩfunctionsimpleMsg($mess){
ﻩecho$mess;
ﻩ}ﻩ
functionskipachange($mess,$url=NULL){
ﻩif(!
is_null($url)){
ﻩﻩecho "<
script>
alert(’"、$mess、”'
location='
”、$url、”’;
/script>
”;
}else{
echo "
script>alert(’”、$mess、”'
location、reload;
/script〉”;
ﻩfunctionrefurbish($mess,$sec=3,$url=NULL){
ﻩﻩif(is_null($url)){
ﻩﻩ$hr =$_SERVER[’_REFERER'];
ﻩecho$mess、’〈br〉'
ﻩﻩﻩecho'页面将于'
、$sec、’秒后跳转.如果没有跳转,请点击这里〈ahref="
、$hr、’”>
返回<
/a〉’;
ﻩﻩecho'
meta—equiv=”refresh”content=”'
、$sec、’;
url='
、$hr、'
”/〉'
ﻩ}else{
ﻩecho$mess、'
<br〉’;
ﻩecho'页面将于'、$sec、'秒后跳转。
如果没有跳转,请点击这里<ahref=”’、$url、'
”〉返回〈/a>'
ﻩﻩecho '
meta —equiv=”Refresh"
content=”'、$sec、'
url='、$url、’"
〉’;
$tm= newTakeMsg();
ﻩinclude_once"
function、php"
$tm->
simpleMsg('
欢迎光临’);
5、实验结果分析与总结
次实验我基本掌握了正则表达式得使用方法,也实现了利用正则表达式限定表单输入值,同时也学会了php获取html表单数据得方法,理解了php中得文件处理得使用方法与类得创建方法,在实验过程中对于函数得编写与调用还比较生疏,需要参考书本与实例来完成,而且对于类得创建与使用还需要多学习。
实 验报告说 明
1、要求每一实验项目书写一份实验报告,并按实验课程归类汇总.
2、实验报告要遵照实验指导书与任课教师得具体要求逐项填写。
3、实验报告正文要求字迹清晰、工整,用钢笔、中性笔或圆珠笔书写。
4、实验报告封面不得有空缺项;
实验项目序号指实验项目编号,如:
实验一。
5、实验运行环境之具体实验室与运行软件。
6、实验内容与步骤如书写不够,可加页。