{"id":1222,"date":"2020-09-23T21:17:59","date_gmt":"2020-09-23T13:17:59","guid":{"rendered":"https:\/\/www.siediyer.cn\/?p=1222"},"modified":"2020-09-23T21:17:59","modified_gmt":"2020-09-23T13:17:59","slug":"php%e5%b8%b8%e7%94%a8%e7%ae%97%e6%b3%95%e6%8e%a8%e8%8d%90","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=1222","title":{"rendered":"PHP\u5e38\u7528\u7b97\u6cd5(\u63a8\u8350)"},"content":{"rendered":"<p><strong>\u4e00\u3001\u5192\u6ce1\u6392\u5e8f<\/strong><\/p>\n<p><strong>\u57fa\u672c\u601d\u60f3\uff1a<\/strong><\/p>\n<p>\u5bf9\u9700\u8981\u6392\u5e8f\u7684\u6570\u7ec4\u4ece\u540e\u5f80\u524d\uff08\u9006\u5e8f\uff09\u8fdb\u884c\u591a\u904d\u7684\u626b\u63cf\uff0c\u5f53\u53d1\u73b0\u76f8\u90bb\u7684\u4e24\u4e2a\u6570\u503c\u7684\u6b21\u5e8f\u4e0e\u6392\u5e8f\u8981\u6c42\u7684\u89c4\u5219\u4e0d\u4e00\u81f4\u65f6\uff0c\u5c31\u5c06\u8fd9\u4e24\u4e2a\u6570\u503c\u8fdb\u884c\u4ea4\u6362\u3002\u8fd9\u6837\u6bd4\u8f83\u5c0f\uff08\u5927\uff09\u7684\u6570\u503c\u5c31\u5c06\u9010\u6e10\u4ece\u540e\u9762\u5411\u524d\u9762\u79fb\u52a8\u3002<\/p>\n<p>\/\/\u5192\u6ce1\u6392\u5e8f<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n \r\n  function mysort($arr)\r\n  {\r\n    for($i = 0; $i &lt; count($arr); $i++)\r\n    {\r\n      $isSort = false;\r\n      for ($j=0; $j&lt; count($arr) - $i - 1; $j++) \r\n      {\r\n        if($arr[$j] &lt; $arr[$j+1])\r\n        {\r\n          $isSort = true;\r\n          $temp = $arr[$j];\r\n          $arr[$j] = $arr[$j+1];\r\n          $arr[$j+1] = $temp ;\r\n        }\r\n      }\r\n      if($isSort)\r\n      {\r\n        break;\r\n      }\r\n    }\r\n    return $arr;\r\n  }\r\n \r\n  $arr = array(3,1,2);\r\n  var_dump(mysort($arr));\r\n?&gt;<\/pre>\n<p><strong>\u4e8c\u3001\u5feb\u901f\u6392\u5e8f<\/strong><\/p>\n<p><strong>\u57fa\u672c\u601d\u60f3\uff1a<\/strong><\/p>\n<p>\u5728\u6570\u7ec4\u4e2d\u6311\u51fa\u4e00\u4e2a\u5143\u7d20\uff08\u591a\u4e3a\u7b2c\u4e00\u4e2a\uff09\u4f5c\u4e3a\u6807\u5c3a\uff0c\u626b\u63cf\u4e00\u904d\u6570\u7ec4\u5c06\u6bd4\u6807\u5c3a\u5c0f\u7684\u5143\u7d20\u6392\u5728\u6807\u5c3a\u4e4b\u524d\uff0c\u5c06\u6240\u6709\u6bd4\u6807\u5c3a\u5927\u7684\u5143\u7d20\u6392\u5728\u6807\u5c3a\u4e4b\u540e\uff0c\u901a\u8fc7\u9012\u5f52\u5c06\u5404\u5b50\u5e8f\u5217\u5206\u522b\u5212\u5206\u4e3a\u66f4\u5c0f\u7684\u5e8f\u5217\u76f4\u5230\u6240\u6709\u7684\u5e8f\u5217\u987a\u5e8f\u4e00\u81f4\u3002<\/p>\n<p>\/\/\u5feb\u901f\u6392\u5e8f<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n  \/\/\u5feb\u901f\u6392\u5e8f\r\n    function quick_sort($arr) \r\n    {\r\n      \/\/\u5148\u5224\u65ad\u662f\u5426\u9700\u8981\u7ee7\u7eed\u8fdb\u884c\r\n      $length = count($arr);\r\n      if($length &lt;= 1) \r\n      {\r\n        return $arr;\r\n      }\r\n     \r\n      $base_num = $arr[0];\/\/\u9009\u62e9\u4e00\u4e2a\u6807\u5c3a \u9009\u62e9\u7b2c\u4e00\u4e2a\u5143\u7d20\r\n \r\n      \/\/\u521d\u59cb\u5316\u4e24\u4e2a\u6570\u7ec4\r\n      $left_array = array();\/\/\u5c0f\u4e8e\u6807\u5c3a\u7684\r\n      $right_array = array();\/\/\u5927\u4e8e\u6807\u5c3a\u7684\r\n      for($i=1; $i&lt;$length; $i++) \r\n      {      \/\/\u904d\u5386 \u9664\u4e86\u6807\u5c3a\u5916\u7684\u6240\u6709\u5143\u7d20\uff0c\u6309\u7167\u5927\u5c0f\u5173\u7cfb\u653e\u5165\u4e24\u4e2a\u6570\u7ec4\u5185\r\n        if($base_num &gt; $arr[$i]) \r\n        {\r\n          \/\/\u653e\u5165\u5de6\u8fb9\u6570\u7ec4\r\n          $left_array[] = $arr[$i];\r\n        } \r\n        else\r\n        {\r\n          \/\/\u653e\u5165\u53f3\u8fb9\r\n          $right_array[] = $arr[$i];\r\n        }\r\n      }\r\n      \/\/\u518d\u5206\u522b\u5bf9 \u5de6\u8fb9 \u548c \u53f3\u8fb9\u7684\u6570\u7ec4\u8fdb\u884c\u76f8\u540c\u7684\u6392\u5e8f\u5904\u7406\u65b9\u5f0f\r\n      \/\/\u9012\u5f52\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570,\u5e76\u8bb0\u5f55\u7ed3\u679c\r\n      $left_array = quick_sort($left_array);\r\n      $right_array = quick_sort($right_array);\r\n      \/\/\u5408\u5e76\u5de6\u8fb9 \u6807\u5c3a \u53f3\u8fb9\r\n      return array_merge($left_array, array($base_num), $right_array);\r\n    }\r\n \r\n    $arr = array(3,1,2);\r\n    var_dump(quick_sort($arr));\r\n \r\n?&gt;<\/pre>\n<p><strong>\u4e09\u3001\u4e8c\u5206\u67e5\u627e<\/strong><\/p>\n<p><strong>\u57fa\u672c\u601d\u60f3\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6570\u636e\u662f\u6309\u5347\u5e8f\u6392\u5e8f\u7684\uff0c\u5bf9\u4e8e\u7ed9\u5b9a\u503cx\uff0c\u4ece\u5e8f\u5217\u7684\u4e2d\u95f4\u4f4d\u7f6e\u5f00\u59cb\u6bd4\u8f83\uff0c\u5982\u679c\u5f53\u524d\u4f4d\u7f6e\u503c\u7b49\u4e8ex\uff0c\u5219\u67e5\u627e\u6210\u529f\uff1b\u82e5x\u5c0f\u4e8e\u5f53\u524d\u4f4d\u7f6e\u503c\uff0c\u5219\u5728\u6570\u5217\u7684\u524d\u534a\u6bb5\u4e2d\u67e5\u627e\uff1b\u82e5x\u5927\u4e8e\u5f53\u524d\u4f4d\u7f6e\u503c\u5219\u5728\u6570\u5217\u7684\u540e\u534a\u6bb5\u4e2d\u7ee7\u7eed\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u4e3a\u6b62\u3002\uff08\u6570\u636e\u91cf\u5927\u7684\u65f6\u5019\u4f7f\u7528\uff09<\/p>\n<p>\/\/\u4e8c\u5206\u67e5\u627e<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n  \/\/\u4e8c\u5206\u67e5\u627e\r\n  function bin_search($arr,$low,$high,$k)\r\n  {\r\n    if($low &lt;= $high)\r\n    {\r\n      $mid = intval(($low + $high)\/2);\r\n      if($arr[$mid] == $k)\r\n      {\r\n        return $mid;\r\n      }\r\n      else if($k &lt; $arr[$mid])\r\n      {\r\n        return bin_search($arr,$low,$mid-1,$k);\r\n      }\r\n      else\r\n      {\r\n        return bin_search($arr,$mid+1,$high,$k);\r\n      }\r\n    }\r\n    return -1;\r\n  }\r\n \r\n  $arr = array(1,2,3,4,5,6,7,8,9,10);\r\n \r\n  print(bin_search($arr,0,9,3));\r\n?&gt;<\/pre>\n<p><strong>\u56db\u3001\u987a\u5e8f\u67e5\u627e<\/strong><\/p>\n<p><strong>\u57fa\u672c\u601d\u60f3\uff1a<\/strong><\/p>\n<p>\u4ece\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\u4e00\u4e2a\u4e00\u4e2a\u5411\u4e0b\u67e5\u627e\uff0c\u5982\u679c\u6709\u548c\u76ee\u6807\u4e00\u81f4\u7684\u5143\u7d20\uff0c\u67e5\u627e\u6210\u529f\uff1b\u5982\u679c\u5230\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4ecd\u6ca1\u6709\u76ee\u6807\u5143\u7d20\uff0c\u5219\u67e5\u627e\u5931\u8d25\u3002<\/p>\n<p>\/\/\u987a\u5e8f\u67e5\u627e<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n  \/\/\u987a\u5e8f\u67e5\u627e\r\n  function seq_search($arr,$n,$k)\r\n  {\r\n    $array[$n] = $k;\r\n    for($i = 0;$i &lt; $n; $i++)\r\n    {\r\n      if($arr[$i] == $k)\r\n      {\r\n        break;\r\n      }\r\n    }\r\n \r\n    if($i &lt; $n)\r\n    {\r\n      return $i;\r\n    }\r\n    else\r\n    {\r\n      return -1;\r\n    }\r\n  }\r\n?&gt;<\/pre>\n<p><strong>\u4e94\u3001\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u80fd\u591f\u904d\u5386\u4e00\u4e2a\u6587\u4ef6\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939<\/strong><\/p>\n<pre class=\"lang:default decode:true \">&lt;?php  \r\n  function my_scandir($dir)\r\n  {\r\n    $files = array();\r\n    if($handle = opendir($dir))\r\n    {\r\n      while (($file = readdir($handle))!== false) \r\n      {\r\n        if($file != '..' &amp;&amp; $file != '.')\r\n        {\r\n          if(is_dir($dir.\"\/\".$file))\r\n          {\r\n            $files[$file]=my_scandir($dir.\"\/\".$file);\r\n          }\r\n          else\r\n          {\r\n            $files[] = $file;\r\n          }\r\n        }\r\n      }\r\n \r\n      closedir($handle);\r\n      return $files;\r\n    }\r\n  }\r\n \r\n  var_dump(my_scandir('..\/'));\r\n?&gt;<\/pre>\n<p><strong>\u516d\u3001\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5c3d\u53ef\u80fd\u9ad8\u6548\u7684\u4ece\u4e00\u4e2a\u6807\u51c6url\u4e2d\u53d6\u51fa\u6587\u4ef6\u7684\u6269\u5c55\u540d<\/strong><\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n  function getExt($url)\r\n  {\r\n    $arr = parse_url($url);\/\/parse_url\u89e3\u6790\u4e00\u4e2a URL \u5e76\u8fd4\u56de\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\uff0c\u5305\u542b\u5728 URL \u4e2d\u51fa\u73b0\u7684\u5404\u79cd\u7ec4\u6210\u90e8\u5206\r\n    \/\/'scheme' =&gt; string 'http' (length=4)\r\n    \/\/'host' =&gt; string 'www.sina.com.cn' (length=15)\r\n    \/\/'path' =&gt; string '\/abc\/de\/fg.php' (length=14)\r\n    \/\/'query' =&gt; string 'id=1' (length=4)\r\n    $file = basename($arr['path']);\/\/ basename\u51fd\u6570\u8fd4\u56de\u8def\u5f84\u4e2d\u7684\u6587\u4ef6\u540d\u90e8\u5206\r\n    $ext = explode('.', $file);\r\n    return $ext[count($ext)-1];\r\n  }\r\n \r\n  print(getExt('http:\/\/www.sina.com.cn\/abc\/de\/fg.html.php?id=1'));\r\n \r\n?&gt;<\/pre>\n<p><strong>\u4e03\u3001\u5b9e\u73b0\u4e2d\u6587\u5b57\u7b26\u4e32\u622a\u53d6\u65e0\u4e71\u7801\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u53ef\u4f7f\u7528mb_substr,\u4f46\u662f\u9700\u8981\u786e\u4fdd\u5728php.ini\u4e2d\u52a0\u8f7d\u4e86php_mbstring.dll\uff0c\u5373\u786e\u4fdd\u201cextension=php_mbstring.dll\u201d\u8fd9\u4e00\u884c\u5b58\u5728\u5e76\u4e14\u6ca1\u6709\u88ab\u6ce8\u91ca\u6389\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u672a\u5b9a\u4e49\u51fd \u6570\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5192\u6ce1\u6392\u5e8f \u57fa\u672c\u601d\u60f3\uff1a \u5bf9\u9700\u8981\u6392\u5e8f\u7684\u6570\u7ec4\u4ece\u540e\u5f80\u524d\uff08\u9006\u5e8f\uff09\u8fdb\u884c\u591a\u904d\u7684\u626b\u63cf\uff0c\u5f53\u53d1\u73b0\u76f8\u90bb\u7684\u4e24\u4e2a\u6570\u503c\u7684\u6b21\u5e8f\u4e0e\u6392\u5e8f\u8981\u6c42 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1222","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1222"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1222\/revisions"}],"predecessor-version":[{"id":1223,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1222\/revisions\/1223"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}