博主信息
JiaJieChen
博文
66
粉丝
3
评论
5
访问量
46815
积分:0
P豆:134

PHP:字符串系统函数,ASCII字符集转换,url解析函数,字符串散列处理

2021年05月06日 15:36:44阅读数:273博客 / JiaJieChen/ PHP

PHP:字符串系统函数,ASCII字符集转换,url解析函数,字符串散列处理

一.字符串系统函数

字符串系统函数 含义
strlen(string $string) 获取字符长度
mt_rand( int $min , int $max) 生成随机数
strcmp($string1,$string2) 比较两个字符串的大小,严格区分大小写
strcasecmp($string1,$string2) 忽略大小写的比较字符串的大小
implode() 将一个一维数组的值转化为字符串
explode() 使用一个字符串分割成一个数组
list() 把数组中的值赋给一组变量
md5() 计算字符串的 MD5 散列值/加密
substr(string $string , int $start , int $length = ?) 返回指定的字符串的子串
str_replace(关联字符,被替换字符,聊天记录) 子字符串替换
ucfirst() 将字符串的首字母转换为大写
DIRECTORY_SEPARATOR 查看当前操作系统支持的路径分隔符

①strlen()获取字符串长度

英文字母/空格/特殊符号为一个字符,文字为三个字符

②mt_rand()随机生成数字

③strcmp()/strcasecmp()比较字符串大小

strcmp比较字符大小,严格要求大小写,适用于验证密码是否一致

strcasecmp比较字符串大小,不区分大小写,适用于验证码验证

④implode()将一维数组转换成字符串

⑤explode()将字符串转换为数组

⑥list()把数组中的值赋给一组变量

索引数组

关联数组

⑦md5()计算字符串的 MD5 散列值/加密

⑧substr()截取指定的子字符串

substr($string,0,0)截取指定的子字符串

⑨str_replace()替换字符串

⑩ucfirst()将字符串的首字母转换为大写

二.ASCII字符集转换

函数 含义
ord ( string $string ) 转换字符串第一个字节为 0-255 之间的值
chr ( int $ascii ) 输入字符集,返回指定的字符

①ord()转换字符串第一个字节为 0-255 之间的值

②chr()输入字符集,返回指定的字符

三.url解析函数,字符串散列处理

函数 含义
parse_str() 将字符串解析成多个变量
parse_url() 解析 URL,返回其组成部分
http_build_query() 生成 URL-encode 之后的请求字符串
base64_encode() 使用 MIME base64 对数据进行编码
file_get_contents() 将整个文件读入生成一个字符串
file_put_contents 将一个字符串写入文件
md5_file() 计算指定文件的 MD5 散列值
password_hash(password,PASSWORD_BCRYPT) 创建密码的散列(hash)
password_verify() 验证密码是否和散列值匹配

①parse_str()将字符串解析成多个变量

②parse_url() 解析 URL,返回其组成部分

③file_get_contents()将整个文件读入生成一个字符串

④base64_encode()使用 MIME base64 对数据进行编码

⑤password_hash(password,PASSWORD_BCRYPT)创建密码的散列(hash)

⑥password_verify()验证密码是否和散列值匹配

⑦md5_file()计算指定文件的 MD5 散列值

⑧file_put_contents将一个字符串写入文件

四.md选择题答案

序列号 对应选项
1-5 D BCD C B A
6-10 AD D B C C
11-14 D B ABC D
15-23 B B A D C D C B A

五.用户注册页面/验证码验证/限制密码长度/两次密码须一致/用户名首位需是字母


html

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>用户注册</title>
  7. <link rel="stylesheet" href="/zwz/0430/captcha/login.css">
  8. </head>
  9. <body>
  10. <div class="box">
  11. <div>
  12. <form action="" method="POST" >
  13. <div class="box1">
  14. <div class="rootzc"><span>用户注册</span></div>
  15. <div class="box2">
  16. <div>用户名:</div>
  17. <div>密码:</div>
  18. <div>再次输入密码:</div>
  19. <div>请输入验证码:</div>
  20. </div>
  21. <div class="box3">
  22. <div class="username"><label for="username"><input type="text" name="username" id="username" placeholder="用户名首位需是字母" minlength="8" maxlength="12" ></label></div>
  23. <div class="password"><label for="password"><input type="password" name="password" id="password" placeholder="输入密码不小于8位" minlength="8" maxlength="12" ></label></div>
  24. <div class="password2"><label for="password2"><input type="password" name="password2" id="password2" placeholder="请再次输入密码" minlength="8" maxlength="12" ></label></div>
  25. <div>
  26. <input type="text" name="captcha" maxlength="4" placeholder="请输入图片中的验证码" >
  27. <img src="gd_captcha.php" onclick="this.src='gd_captcha.php?'+new Date().getTime();">
  28. <div id="error_msg">&nbsp; </div>
  29. </div>
  30. </div>
  31. </div>
  32. </form>
  33. </div>
  34. <div class="btn">
  35. <button name="button" class="button" >注册</button>
  36. </div>
  37. </div>
  38. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  39. <script type="text/javascript" src="/zwz/0430/captcha/ajax.js"></script>
  40. </body>
  41. </html>

CSS

  1. /*初始化*/
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. /*设置body背景*/
  8. body {
  9. background: url(/zwz/0430/captcha/xkimg.jpg);
  10. background-size: 100% 100%;
  11. background-repeat: no-repeat;
  12. }
  13. /*主体盒子样式*/
  14. .box {
  15. width: 400px;
  16. height: 389px;
  17. color: white;
  18. background-color: rgb(0, 0, 0, 0.6);
  19. margin: 80px auto;
  20. /* display: grid; */
  21. /* grid-template-rows: 120px 80px; */
  22. }
  23. /*用户注册样式*/
  24. .rootzc {
  25. grid-column: span 2;
  26. background-color: greenyellow;
  27. color: #000;
  28. font-size: 1.2em;
  29. font-weight: bold;
  30. display: flex;
  31. justify-content: center;
  32. align-items: center;
  33. width: 100%;
  34. height: 100%;
  35. }
  36. /*box1盒子样式*/
  37. .box1 {
  38. display: grid;
  39. grid-template-columns: 120px 1fr;
  40. grid-template-rows: repeat(1, 40px 1fr);
  41. gap: 5px;
  42. place-items: center;
  43. }
  44. /*box2盒子样式*/
  45. .box2 {
  46. grid-area: 2/1;
  47. display: grid;
  48. grid-template-rows: repeat(7, 45px);
  49. place-items: center end;
  50. }
  51. /*box3盒子样式*/
  52. .box3 {
  53. grid-area: 2/2;
  54. display: grid;
  55. grid-template-rows: repeat(7, 45px);
  56. place-items: center left;
  57. position: relative;
  58. }
  59. /*注册按钮样式*/
  60. .button {
  61. width: 100%;
  62. height: 30px;
  63. background-color: white;
  64. }
  65. .button:hover {
  66. background-color: greenyellow;
  67. cursor: pointer;
  68. }
  69. /*提示样式*/
  70. #error_msg {
  71. position: relative;
  72. bottom: -15px;
  73. }

gd验证码

  1. <?php
  2. //session_start — 启动新会话或者重用现有会话
  3. session_start();
  4. //imagecreatetruecolor — 新建一个真彩色图像
  5. $image = imagecreatetruecolor(100, 30);
  6. //imagecolorallocate — 为一幅图像分配颜色
  7. $bgcolor = imagecolorallocate($image, 255, 255, 255);
  8. // imagefill — 区域填充
  9. imagefill($image, 0, 0, $bgcolor);
  10. $content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  11. $captcha = "";
  12. for ($i = 0; $i < 4; $i++) {
  13. // 字体大小
  14. $fontsize = 10;
  15. // 字体颜色 //imagecolorallocate — 为一幅图像分配颜色
  16. $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
  17. // 设置字体内容 随机$content下标
  18. $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
  19. //空字符串 保存随机的字体 拼接4次
  20. $captcha .= $fontcontent;
  21. // 显示的坐标
  22. $x = ($i * 100 / 4) + mt_rand(5, 10);
  23. $y = mt_rand(5, 10);
  24. // 填充内容到画布中 // imagefill — 区域填充
  25. imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
  26. }
  27. $_SESSION["captcha"] = $captcha;
  28. //4.3 设置背景干扰元素
  29. for ($$i = 0; $i < 200; $i++) {
  30. $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
  31. imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor);
  32. }
  33. //4.4 设置干扰线
  34. for ($i = 0; $i < 3; $i++) {
  35. $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
  36. imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor);
  37. }
  38. //5.向浏览器输出图片头信息
  39. header('content-type:image/png');
  40. //6.输出图片到浏览器
  41. imagepng($image);
批改老师:灭绝师太灭绝师太

批改状态:合格

老师批语:

版权申明:本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
  • php url组的方法:首先获取url;然后通过“parse_str”把查询到变量中,并存储变量到一个组中;最后通过“var_dump”打印出后的组即可。
    php json汉乱码的决方法:1、保证据库和前后台php页面编码一致;2、利用urlencode与decodeURL进行URL编码;3、利用编码进行之间的