Blogger Information
Blog 38
fans 0
comment 0
visits 22647
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用递归函数删除指定目录,字符串操作函数以及一些函数的使用
一个好人
Original
487 people have browsed it

使用递归函数删除指定目录

成功删除0407目录

  1. if(!function_exists('delete_dir_file')){
  2. function delete_dir_file($dir){
  3. $flag = false;
  4. if(is_dir($dir)){
  5. if($handle = opendir($dir)){
  6. while(($file = readdir($handle)) !== false){
  7. // echo $file.'<br>';
  8. if($file !=='.'&&$file !=='..'){
  9. if(is_dir($dir.DIRECTORY_SEPARATOR.$file)){
  10. delete_dir_file($dir.DIRECTORY_SEPARATOR.$file);
  11. }else unlink($dir.DIRECTORY_SEPARATOR.$file);
  12. }
  13. }
  14. }
  15. closedir($handle);
  16. if(rmdir($dir)) $flag = true;
  17. }
  18. return $flag;
  19. }
  20. }
  21. $dir = __DIR__.DIRECTORY_SEPARATOR.'0407';
  22. // echo $dir;
  23. if(delete_dir_file($dir)){
  24. echo json_encode(['msg'=>'删除成功','code'=>0],320);
  25. }else{
  26. echo json_encode(['msg'=>'删除失败','code'=>1],320);

实现产品多级分类并在前端显示

  1. $db = new PDO('mysql:host=localhost;dbname=laravel','root','root');
  2. $RES = $db->query('select `id`,`pid`,`name` from `cates` where `status`=1;')->fetchAll(PDO::FETCH_ASSOC);
  3. printf('<pre>%s</pre>',print_r($RES,true));
  4. function ruleLayer($rule, $pid = 0){
  5. $arr = [];
  6. foreach($rule as $v){
  7. if($v['pid']==$pid){
  8. $v['child'] = ruleLayer($rule, $v['id']);
  9. $arr[] = $v;
  10. }
  11. }
  12. return $arr;
  13. }
  14. $result = ruleLayer($RES);
  15. printf('<pre>%s</pre>',print_r($result,true));
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="zh-CN">
  19. <head>
  20. <meta charset="UTF-8">
  21. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  22. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  23. <title>Document</title>
  24. </head>
  25. <body>
  26. <ul class="first">
  27. <?php foreach($result as $v): extract($v)?>
  28. <li>
  29. <?=$name?>
  30. <ul>
  31. <?php if(!empty($child)):
  32. foreach($child as $v1): extract($v1)?>
  33. <li>
  34. <?=$name?>
  35. <ul>
  36. <?php if(!empty($child)):
  37. foreach($child as $v2): extract($v2)?>
  38. <li>
  39. <?=$name?>
  40. <ul>
  41. <?php if(!empty($child)):
  42. foreach($child as $v3): extract($v3)?>
  43. <li> <?=$name?> </li>
  44. <?php endforeach;
  45. endif?>
  46. </ul>
  47. </li>
  48. <?php endforeach;
  49. endif?>
  50. </ul>
  51. </li>
  52. <?php endforeach;
  53. endif?>
  54. </ul>
  55. </li>
  56. <?php endforeach?>
  57. </ul>
  58. </body>
  59. </html>

字符串操作函数

  1. $arr = ['html', 'css', 'vue'];
  2. $str = implode('|', $arr);
  3. // echo $str;
  4. // 合并,字符串->数组 explode
  5. // echo $_SERVER['REQUEST_URI'];
  6. $pathinfo = (explode("/", $_SERVER['REQUEST_URI']));
  7. echo array_pop($pathinfo); //弹出方法名称
  8. // 字符串的替换 str_replace($被替换内容, $替换内容,检测文档,替换次数)
  9. $search = ['广告','转账'];
  10. $chatMSG = '本公司代理各类广告,可转账。';
  11. $res = str_replace($search, "**",$chatMSG,$COUNT);
  12. echo $res, $COUNT;
  13. if($COUNT == 0){
  14. // ws->sendMessage();
  15. }else{
  16. // 警告功能
  17. }
  18. // 字符串查找、截取
  19. // 随机形成验证码
  20. // echo rand();
  21. $str = md5(rand());
  22. $code = substr($str, 0,4);
  23. $color = '#'.substr(md5(mt_rand()), 0, 6);
  24. echo "<h1 style='color:{$color}'>{$code}</h1>";
  25. echo mb_substr('你吃饭了吗?', 1, 2);

有的电脑是REQUEST_URI,而不是DOCUMENT_URI

一些函数的使用

http_build_query、urlencode、 urldecode、base64_encode、base64_decode

  1. // url函数
  2. echo urlencode('韩寒');
  3. echo urldecode('%E9%9F%A9%E5%AF%92');
  4. // http_build_query(),生产urlencode之后的请求字符串
  5. $params = [
  6. 'city' => '北京',
  7. 'key' => '298uejkfl',
  8. ];
  9. $paramsString = http_build_query($params);
  10. echo $paramsString;
  11. ob_clean();
  12. // base64_encode()使用mime base64 对二进制字符串进行编码 ,目的是为了使二进制数据可以通过非纯8-bit的传输层传输,例如电子邮件的主体。
  13. // header('content-type:image/jpeg');
  14. $file = base64_encode(file_get_contents("https://img-nos.yiyouliao.com/alph/a21a0c67a67682baddf116e04826ef0d.jpeg?yiyouliao_channel=1536235174142947329_image"));
  15. echo $file;
  16. ?>
  17. <body>
  18. <h1>您好!</h1>
  19. <img src="data:image/jpeg;base64,<?=$file?>" alt="">
  20. </body>

总结:

递归函数挺有用,用于产品分类也很实用;字符串操作也是基本技能,必须掌握;后面的几个函数之前没接触过,先了解一下吧!

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