1.产生指定长度的随机字符串
<?php
function random_string($length, $max=FALSE){
if (is_int($max) && $max > $length){
$length = mt_rand($length, $max);
}
$output = '';
for ($i=0; $i<$length; $i++){
$which = mt_rand(0,2);
if ($which === 0){
$output .= mt_rand(0,9);
} elseif ($which === 1) {
$output .= chr(mt_rand(65,90));
} else {
$output .= chr(mt_rand(97,122));
}
}
return $output;
}
echo random_string(8)."n";
echo random_string(8, 11)."n";
2.获取文件夹大小
<?php
function dirSize($directoty){
$dir_size = 0;
if($dir_handle = @opendir($directoty))
{
while($filename = readdir($dir_handle)){
$subFile = $directoty.DIRECTORY_SEPARATOR.$filename;
if($filename == '.' || $filename == '..'){
continue;
}elseif (is_dir($subFile)) {
$dir_size += dirSize($subFile);
}elseif (is_file($subFile)){
$dir_size += filesize($subFile);
}
}
closedir($dir_handle);
}
return ($dir_size);
}
$dir_size=dirSize( getcwd() );
echo round($dir_size/pow(1024,1), 2)."KB";
?>
3.实现简单的聊天功能
<?php
if(!extension_loaded('sockets')) {
if(strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
dl('php_sockets.dll');
}else {
dl('sockets.so');
}
}
// set_time_limit(0);
$address = '127.0.0.1';
$port = '123123';
$welcome_msg = "rnWelcome to the PHP Test Server.";
$welcome_msg .= "rnTo quit, type 'quit'. To shutdown the server just type 'shutdown'.rn";
$msg_len = 1024;
$msg_eof = "n"; //信息结束标记
$usleep = 1000*100; //间隔周期
$connections = array();
$commonProtocol = getprotobyname("tcp");
if(($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "rn";
exit;
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "rn";
exit;
}
if (socket_listen($sock, 15) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "rn";
exit;
}
socket_set_nonblock($sock) or die('failed to set nonblock');
// Accept any incoming connections to the server
do {
@ $connection = socket_accept($sock);
if($connection) {
$connections[] = array('con'=>$connection, 'bufs'=>'');
socket_write($connection, $welcome_msg, strlen($welcome_msg));
unset($connection);
}
$talk_bufs = '';
foreach($connections as $xid => & $client) {
unset($bufs);
$connection = $client['con'];
$bufs = &$client['bufs'];
if(false === socket_write($connection, $talkback, strlen($talkback))) {
echo "socket_write() failed: reason: " . socket_strerror(socket_last_error($connection)) . "rn";
socket_close($connection);
unset($connections[$xid]);
continue;
}
// if (false === ($buf = socket_read($connection, $msg_len, PHP_NORMAL_READ))) {
if (false === ($buf = socket_read($connection, $msg_len))) {
continue;
}
$bufs .= $buf;
if(false === strpos($bufs, $msg_eof)) {
continue;
}
$buf = substr($bufs,0,(strrpos($bufs, $msg_eof)));
$bufs = substr(strrchr($bufs, $msg_eof), 1);
if (!$buf = trim($buf)) {
continue;
}
// 判断刷屏
if(strrpos($buf, $msg_eof) || (strlen($buf) > $msg_len)) {
$buf = 'quit';
}
if ($buf == 'quit' || $buf == 'exit') {
socket_close($connection);
unset($connections[$xid]);
continue;
}
if ($buf == 'shutdown') {
socket_close($connection);
break 2;
}
$talk_bufs .= "PHP: $xid said '$buf'.rn";
}
$talkback = $talk_bufs;
usleep($usleep);
} while(true);
socket_close($sock);
// 使用方法 telnet 连接 127.0.0.1 123123 端口,可以绑定外部IP
// 使用的时候最好在 cli 方式下执行
4.自定义匹配函数
<?php
/* 自定义匹配函数 Version 1*/
function isMatch($string1, $string2) {
$array1 = array();
$array2 = array();
// 将字符串分解为数组
for ($i=0; $i<strlen($string1); $i++)
$array1[] = $string1[$i];
for ($i=0; $i<strlen($string2); $i++)
$array2[] = $string2[$i];
// 将数组转换为元素频度数组
$array1_count = array_count_values($array1);
$array2_count = array_count_values($array2);
// 将数组按键值升序排序
ksort($array1_count);
ksort($array2_count);
if ($array1_count == $array2_count)
echo 'match success!';
else
echo 'match fail!';
}
// 测试
isMatch('abcda', 'adabc'); // 打印 'match success!'
5.文件切割的PHP脚本
<?php
$fp=fopen("fxxk.jpg","rb");
$sum=0;
for($i=0; $i<9999999999; $i++) {
$temp=fread($fp,1024*199); //199KB每块,因为非注册版的winhex只能编辑保存200KB以下的文件
if(strlen($temp)==0) {
break;
}
$temp2=fgets($fp);
if(strlen($temp2)!=0) {
$temp.=$temp2;
}
$f=fopen("temp{$i}.jpg","wb");
fwrite($f,$temp);
fclose($f);
}
echo "OK.";
待续……
《 “一些PHP代码片段_1” 》 有 2 条评论
A PHP MySQL PDO class(一个PHP访问MySQL操作的封装类)
https://github.com/lincanbin/PHP-PDO-MySQL-Class
https://github.com/offensive-security/masscan-web-ui/blob/master/lib/class.db.php
一个PHP资源列表,内容包括:库、框架、模板、安全、代码分析、日志、第三方库、配置工具、Web 工具、书籍、电子书、经典博文等等
https://github.com/CraryPrimitiveMan/awesome-php-zh_CN
https://github.com/ziadoz/awesome-php