{"id":1194,"date":"2014-09-10T15:18:53","date_gmt":"2014-09-10T15:18:53","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1194"},"modified":"2014-09-10T15:18:53","modified_gmt":"2014-09-10T15:18:53","slug":"%e4%b8%80%e4%ba%9bphp%e4%bb%a3%e7%a0%81%e7%89%87%e6%ae%b5_1","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1194.html","title":{"rendered":"\u4e00\u4e9bPHP\u4ee3\u7801\u7247\u6bb5_1"},"content":{"rendered":"<h6>1.\u4ea7\u751f\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u7b26\u4e32<\/h6>\n<pre class=\"lang:default decode:true \">&lt;?php\n\nfunction random_string($length, $max=FALSE){\n\tif (is_int($max) &amp;&amp; $max &gt; $length){\n\t\t$length = mt_rand($length, $max);\n\t}\n\t$output = '';\n\n\tfor ($i=0; $i&lt;$length; $i++){\n\t\t$which = mt_rand(0,2);\n\n\t\tif ($which === 0){\n\t\t\t$output .= mt_rand(0,9);\n\t\t} elseif ($which === 1) {\n\t\t\t$output .= chr(mt_rand(65,90));\n\t\t} else {\n\t\t\t$output .= chr(mt_rand(97,122));\n\t\t}\n\t}\n\treturn $output;\n}\necho random_string(8).\"n\";\necho random_string(8, 11).\"n\";<\/pre>\n<h6>2.\u83b7\u53d6\u6587\u4ef6\u5939\u5927\u5c0f<\/h6>\n<pre class=\"lang:default decode:true \">&lt;?php\n\tfunction dirSize($directoty){\n\t\t$dir_size = 0;\n\t\tif($dir_handle = @opendir($directoty))\n\t\t{\n\t\t\twhile($filename = readdir($dir_handle)){\n\t\t\t\t$subFile = $directoty.DIRECTORY_SEPARATOR.$filename;\n\t\t\t\tif($filename == '.' || $filename == '..'){\n\t\t\t\t\tcontinue;\n\t\t\t\t}elseif (is_dir($subFile)) {\n\t\t\t\t\t$dir_size += dirSize($subFile);\n\t\t\t\t}elseif (is_file($subFile)){\n\t\t\t\t\t$dir_size += filesize($subFile);\n\t\t\t\t}\n\t\t\t}\n\t\t\tclosedir($dir_handle);\n\t\t}\n\t\treturn ($dir_size);\n\t}\n\n\t$dir_size=dirSize( getcwd() );\n\techo round($dir_size\/pow(1024,1), 2).\"KB\";\n?&gt;<\/pre>\n<h6>3.\u5b9e\u73b0\u7b80\u5355\u7684\u804a\u5929\u529f\u80fd<\/h6>\n<pre class=\"lang:default decode:true\">&lt;?php\nif(!extension_loaded('sockets')) {\n\tif(strtoupper(substr(PHP_OS, 0, 3)) == \"WIN\") {\n\t\tdl('php_sockets.dll');\n\t}else {\n\t\tdl('sockets.so');\n\t}\n}\n\n\/\/ set_time_limit(0);\n$address = '127.0.0.1';\n$port = '123123';\n$welcome_msg = \"rnWelcome to the PHP Test Server.\";\n$welcome_msg .= \"rnTo quit, type 'quit'. To shutdown the server just type 'shutdown'.rn\";\n$msg_len = 1024;\n$msg_eof = \"n\"; \/\/\u4fe1\u606f\u7ed3\u675f\u6807\u8bb0\n$usleep = 1000*100; \/\/\u95f4\u9694\u5468\u671f\n$connections = array();\n\n$commonProtocol = getprotobyname(\"tcp\");\n\nif(($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {\n\techo \"socket_create() failed: reason: \" . socket_strerror(socket_last_error()) . \"rn\";\n\texit;\n}\nif (socket_bind($sock, $address, $port) === false) {\n\techo \"socket_bind() failed: reason: \" . socket_strerror(socket_last_error($sock)) . \"rn\";\n\texit;\n}\nif (socket_listen($sock, 15) === false) {\n\techo \"socket_listen() failed: reason: \" . socket_strerror(socket_last_error($sock)) . \"rn\";\n\texit;\n}\n\nsocket_set_nonblock($sock) or die('failed to set nonblock');\n\n\/\/ Accept any incoming connections to the server\ndo {\n\t@ $connection = socket_accept($sock);\n\tif($connection) {\n\t\t$connections[] = array('con'=&gt;$connection, 'bufs'=&gt;'');\n\t\tsocket_write($connection, $welcome_msg, strlen($welcome_msg));\n\t\tunset($connection);\n\t}\n\n\t$talk_bufs = '';\n\tforeach($connections as $xid =&gt; &amp; $client) {\n\t\tunset($bufs);\n\n\t\t$connection = $client['con'];\n\t\t$bufs = &amp;$client['bufs'];\n\n\t\tif(false === socket_write($connection, $talkback, strlen($talkback))) {\n\t\t\techo \"socket_write() failed: reason: \" . socket_strerror(socket_last_error($connection)) . \"rn\";\n\t\t\tsocket_close($connection);\n\t\t\tunset($connections[$xid]);\n\t\t\tcontinue;\n\t\t}\n\n\t\t\/\/ if (false === ($buf = socket_read($connection, $msg_len, PHP_NORMAL_READ))) {\n\t\tif (false === ($buf = socket_read($connection, $msg_len))) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t$bufs .= $buf;\n\t\tif(false === strpos($bufs, $msg_eof)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t$buf = substr($bufs,0,(strrpos($bufs, $msg_eof)));\n\t\t$bufs = substr(strrchr($bufs, $msg_eof), 1);\n\n\t\tif (!$buf = trim($buf)) {\n\t\t\tcontinue;\n\t\t}\n\t\t\/\/ \u5224\u65ad\u5237\u5c4f\n\t\tif(strrpos($buf, $msg_eof) || (strlen($buf) &gt; $msg_len)) {\n\t\t\t$buf = 'quit';\n\t\t}\n\t\tif ($buf == 'quit' || $buf == 'exit') {\n\t\t\tsocket_close($connection);\n\t\t\tunset($connections[$xid]);\n\t\t\tcontinue;\n\t\t}\n\t\tif ($buf == 'shutdown') {\n\t\t\tsocket_close($connection);\n\t\t\tbreak 2;\n\t\t}\n\n\t\t$talk_bufs .= \"PHP: $xid said '$buf'.rn\";\n\t}\n\n\t$talkback = $talk_bufs;\n\tusleep($usleep);\n} while(true);\nsocket_close($sock);\n\n\/\/ \u4f7f\u7528\u65b9\u6cd5 telnet \u8fde\u63a5 127.0.0.1 123123 \u7aef\u53e3,\u53ef\u4ee5\u7ed1\u5b9a\u5916\u90e8IP\n\/\/ \u4f7f\u7528\u7684\u65f6\u5019\u6700\u597d\u5728 cli \u65b9\u5f0f\u4e0b\u6267\u884c<\/pre>\n<h6>4.\u81ea\u5b9a\u4e49\u5339\u914d\u51fd\u6570<\/h6>\n<pre class=\"lang:default decode:true \">&lt;?php\n\/* \u81ea\u5b9a\u4e49\u5339\u914d\u51fd\u6570 Version 1*\/\n\nfunction isMatch($string1, $string2) {\n    $array1 = array();\n    $array2 = array();\n\n    \/\/ \u5c06\u5b57\u7b26\u4e32\u5206\u89e3\u4e3a\u6570\u7ec4\n    for ($i=0; $i&lt;strlen($string1); $i++)\n        $array1[] = $string1[$i];\n\n    for ($i=0; $i&lt;strlen($string2); $i++)\n        $array2[] = $string2[$i];\n\n    \/\/ \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5143\u7d20\u9891\u5ea6\u6570\u7ec4\n    $array1_count = array_count_values($array1);\n    $array2_count = array_count_values($array2);\n\n    \/\/ \u5c06\u6570\u7ec4\u6309\u952e\u503c\u5347\u5e8f\u6392\u5e8f\n    ksort($array1_count);\n    ksort($array2_count);\n\n    if ($array1_count == $array2_count)\n        echo 'match success!';\n    else\n        echo 'match fail!';\n}\n\/\/ \u6d4b\u8bd5\nisMatch('abcda', 'adabc');  \/\/ \u6253\u5370 'match success!'<\/pre>\n<h6>5.\u6587\u4ef6\u5207\u5272\u7684PHP\u811a\u672c<\/h6>\n<pre class=\"lang:default decode:true \">&lt;?php\n\n$fp=fopen(\"fxxk.jpg\",\"rb\");\n$sum=0;\nfor($i=0; $i&lt;9999999999; $i++) {\n\t$temp=fread($fp,1024*199);\t\/\/199KB\u6bcf\u5757\uff0c\u56e0\u4e3a\u975e\u6ce8\u518c\u7248\u7684winhex\u53ea\u80fd\u7f16\u8f91\u4fdd\u5b58200KB\u4ee5\u4e0b\u7684\u6587\u4ef6\n\tif(strlen($temp)==0) {\n\t\tbreak;\n\t}\n\t$temp2=fgets($fp);\n\tif(strlen($temp2)!=0) {\n\t\t$temp.=$temp2;\n\t}\n\t$f=fopen(\"temp{$i}.jpg\",\"wb\");\n\tfwrite($f,$temp);\n\tfclose($f);\n}\necho \"OK.\";<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5f85\u7eed\u2026\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u4ea7\u751f\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u7b26\u4e32 &lt;?php function random_string($length, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[48],"class_list":["post-1194","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-php"],"views":3250,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1194"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1194\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}