批改状态:合格
老师批语:掌握了字符中处理函数, 在开发中可以节省大量的时间
$_SERVER自变量获取系统的值
<?php#字符串函数// 1.字符串打印输出函数// a.echo输出字符串$name = 'angle';//字符串使用双引号时可以解析其中的变量,变量使用{变量名称}//如果输出不含变量的字符串,推荐使用单引号echo "我的名字是:{$name}",'<br>';// b.sprintf 返回格式化后的字符串到变量中$table = 'tb_user';$userName = 'angle';$passWord = 'wj880827';$showCount = 10;//可以使用sprintf(字符串格式,变量)返回一个sql查询字符串$sqlStr = sprintf('select * from `%s` where username=%s and password=%s limit %d',$table,$userName,$passWord,$showCount);echo $sqlStr,'<br>';//c.vsprintf 参数是数组,返回格式化后的字符串$sqlArr = ['tb_user','hugn','hugn124',10];$sqlStr = vsprintf('select * from `%s` where username=%s and password=%s limit %d',$sqlArr);echo $sqlStr,'<br>';//d.vfprintf把格式化后的字符串写入文件流中,参数可以使用数组//打开一个文件fopen(文件名,打开方式),如果文件不存在则新建一个$fileHandle = fopen('sql.txt','w');//用写的方式打开sql.txt文件$sqlArr = ['tb_user','angle',1];$sqlArr1 = ['tb_student','peter'];vfprintf($fileHandle,"select * from `%s` where username=%s and isadmin=%d\n",$sqlArr);//把格式化后的字符串写入sql.txtvfprintf($fileHandle,'select * from `%s` where name=%s',$sqlArr1);//继续写入格式化后的字符串echo file_get_contents('sql.txt');//file_get_contents(文件名)获取文件中的内容echo '<hr>';// 2.分割查询与替换字符串函数//a.把数组分割转换为字符串implode()$arrStr = ['angle','女',32,true];$stuInfo = implode(',',$arrStr);//把数组中的每个元素转换为字符串,并且用‘,’号分割echo $stuInfo,'<br>';//b.把字符串转换为数组explode(数组元素之间的分割符,要分割的字符串)$stuInfo = 'angle,女,32,1';$arrStu = explode(',',$stuInfo);//explode(数组元素之间的分割符,要分割的字符串)$pStr = print_r($arrStu,true);//printr()转换字符串到变量里echo printf('<pre>%s</pre>',$pStr);//数组格式化输出echo vsprintf("姓名:%s 性别:%s 年龄:%s 是否在职:%s",$arrStu),'<br>';//数组格式化输出为一个字符串//c.字符串截取substr()$str = 'my name is wangjiao i am a gril i am 32';echo substr($str,0,10),'<br>';echo substr($str,-8,8),'<br>';echo substr_count($str,'am'),'<br>';//e.查找字符串中子串的出现次数substr_count()echo substr_replace($str,'34',-2,2),'<br>';//d.字符串替换substr_replace//f.将字符串填充到指定长度str_pad()$str = 'wangjiao';echo str_pad($str,15,"*",STR_PAD_BOTH);//将字符串填充到指定的长度//g.字符串查找并替换,支持数组str_replace()$seaStr = ['销售','微信','徽商','带货'];$relStr = ['**','&&','##','--'];$conStr = ['销售化妆品','我的微信号是:angle','徽商是时代产物','直播带货'];$repConstr = str_replace($seaStr,$relStr,$conStr);echo sprintf('<pre>%s</pre>',print_r($repConstr,true));//h.去掉字符串中的html和php标签strip_tags()echo '<h2>php中文网</h2>','<br>';echo strip_tags('<h2>php中文网</h2>'),'<br>';echo '<hr>';// 3.URL处理函数//a. parse_str()// parse_str(): 解析查询字符串//http://localhost/php11/0422/index.php?type=fruit&page=2 url地址$queStr = $_SERVER['QUERY_STRING'];//$_SERVER['QUERY_STRING']获取url地址的查询字符串echo $queStr;//type=fruit&page=2获取的查询字符串parse_str( $queStr, $getStr);//将获取的查询字符串转换为一个数组printf('<pre>%s</pre>',print_r($getStr,true));//打印数组中的值//b. parse_url():解析url地址$urlStr = 'http://localhost/php11/0422/index.php?type=fruit&page=2';echo $urlStr;$urlArr = parse_url($urlStr);//不添加第二个参数,默认返回协议,主机名,脚本路径,查询字符串$queStr = parse_url($urlStr,PHP_URL_QUERY);//加第二个参数返回查询字符串printf('<pre>%s</pre>',print_r($urlArr,true));//打印解析URL后数组中的值echo $queStr,'<br>';//输入查询字符串//c. http_build_query()// 把一个数组解析为一个查询字符串$queArr = ['id' => '1001','class' => '05-03','name' => 'angle'];$queStr = http_build_query($queArr);$hostStr = $_SERVER['HTTP_HOST'];//返回http主机名或域名$pathStr = $_SERVER['SCRIPT_NAME'];//返加当前执行脚本的路径和文件名$proStr = $_SERVER['SERVER_PROTOCOL'];//返回协议$urlStr = substr($proStr,0,4).'://'.$hostStr.$pathStr.'?'.$queStr;echo $urlStr;?>
-运行效果图

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号