Blogger Information
Blog 9
fans 0
comment 0
visits 7016
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
字符串函数与动态表格
choa fan
Original
548 people have browsed it

字符串函数

strlen() 获取字符串长度

strtoupper() 将字符串转化为大写
strtolower() 将字符串转化为小写

trim() — 去除字符串首尾处的空白字符(或者其他字符)

explode() — 使用一个字符串分割另一个字符串 explode(“ “, $pizza);

implode() — 将一个一维数组的值转化为字符串 implode(“,”, $array);

empty() 判断是否为空

str_replace() — 字符串替换 区分大小写 str_replace(“网站开发”, “学习”, “网站开发PHP”);

str_ireplace()— 字符串替换 不区分大小写

addslashes() — 在每个双引号(”)前添加反斜杠

  1. $mystring = "abc'";
  2. echo addslashes($mystring);//abc\'

stripslashes() — 删除反斜杠

  1. $mystring = "abc\'";
  2. echo stripslashes($mystring);//abc\'

htmlspecialchars() 昵称和留言时会用到

  1. $mystring = "<script>alert('123');</script>";
  2. // &lt;script&gt;alert('123');&lt;/script&gt;
  3. echo htmlspecialchars($mystring); //在浏览器上原样输出<script>alert('123');</script>;

htmlspecialchars_decode()

  1. $mystring = "&lt;script&gt;alert('123');&lt;/script&gt;";
  2. echo $mystring;//不会弹窗,只会浏览器输出 <script>alert('123');</script>
  3. echo htmlspecialchars_decode($mystring);//js弹窗

动态表格

  1. <?php
  2. function table(array $arr,$head,int $width=100){
  3. $table = '<table border="1">';
  4. $table .= ' <thead>';
  5. $table .= ' <tr style="background-color: #7ac0c3;color: #fff">';
  6. foreach($head as $head_k=>$head_v){
  7. $table .= '<th width="'. $width .'">'. $head_v .'</th>';
  8. }
  9. $table .= ' </tr>';
  10. $table .= ' </thead>';
  11. $table .= ' <tbody style="text-align: center;">';
  12. foreach($arr as $k=>$v){
  13. $table .= ' <tr>';
  14. foreach($v as $kk=>$vv){
  15. $table .= ' <td>'. $vv .'</td>';
  16. }
  17. $table .= ' </tr>';
  18. }
  19. $table .= ' </tbody>';
  20. $table .= '</table>';
  21. return $table;
  22. }
  23. ?>
  24. <!DOCTYPE html>
  25. <html lang="en">
  26. <head>
  27. <meta charset="UTF-8">
  28. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  29. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  30. <title>Document</title>
  31. <style>
  32. table {
  33. border-collapse:collapse;
  34. }
  35. tr {
  36. background-color: #c8e6e8;
  37. }
  38. tr:nth-child(2n) {
  39. background-color: #e6f1f3;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <?php echo table($arr,$head); ?>
  45. </body>
  46. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post