compact
<?php$data['id'] = 12;$data2['a'] = 'a';$data2['b'] = 'b';$a = 1; $b = 2;$data = compact("a", "b", 'data2', 'data');print_r($data);/*****************************/Array([a] => 1[b] => 2[data2] => Array([a] => a[b] => b)[data] => Array([id] => 12))/******************************/
参考:http://php.net/manual/zh/function.compact.php
\WWW\bi8_game>php think run -p 8080
global
<?php$variable = 5;function name(){global $variable;$value = $variable + 5;return $value;}echo name(2); // 5
生成六位邀请码
// 生成邀请码function generate_invite_code($length = 6){$chars = str_shuffle("ABCDEFGHJKMNPQRSTUVWXYZ23456789");$randomString = substr($chars, 0, $length);return $randomString;}
https://stackoverflow.com/questions/39228149/how-to-create-6-digit-otp-with-2-digits-and-4-alphabets
对称加密
First, you will need to generate a pseudo-random string of bytes that you will use as a 256 bit encryption key. The requested length will be 32 (since 32 bytes = 256 bits). If you echo out the key, you will notice that your browser chokes. In order to avoid possible corruption when storing the key in a file or database, we will base64_encode it. Use the code below to generate your key(s). The key will need to be saved since the data has to be encoded and decoded using the same key. If your encrypted data is being stored in a database, your encryption key will most likely need to be stored in a configuration file.
// 生成密匙保存到config文件$encryption_key_256bit = base64_encode(openssl_random_pseudo_bytes(32));
Now that we have our key, we will create the encryption function. We will pass our data to be encoded, and our key, into the function. In addition to our key, there is a secondary random string we will create and use called an initialization vector (IV) that helps to help strengthen the encryption.
function my_encrypt($data, $key) {// Remove the base64 encoding from our key$encryption_key = base64_decode($key);// Generate an initialization vector$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));// Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);// The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)return base64_encode($encrypted . '::' . $iv);}
Now for the decryption function:
function my_decrypt($data, $key) {// Remove the base64 encoding from our key$encryption_key = base64_decode($key);// To decrypt, split the encrypted data from our IV - our unique separator used was "::"list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);}
Putting it all together:
<?php//$key is our base64 encoded 256bit key that we created earlier. You will probably store and define this key in a config file.$key = 'bRuD5WYw5wd0rdHR9yLlM6wt2vteuiniQBqE70nAuhU=';function my_encrypt($data, $key) {// Remove the base64 encoding from our key$encryption_key = base64_decode($key);// Generate an initialization vector$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));// Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);// The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)return base64_encode($encrypted . '::' . $iv);}function my_decrypt($data, $key) {// Remove the base64 encoding from our key$encryption_key = base64_decode($key);// To decrypt, split the encrypted data from our IV - our unique separator used was "::"list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);}//our data to be encoded$password_plain = 'abc123';echo $password_plain . "<br>";//our data being encrypted. This encrypted data will probably be going into a database//since it's base64 encoded, it can go straight into a varchar or text database field without corruption worry$password_encrypted = my_encrypt($password_plain, $key);echo $password_encrypted . "<br>";//now we turn our encrypted data back to plain text$password_decrypted = my_decrypt($password_encrypted, $key);echo $password_decrypted . "<br>";
The code above will output the following. Note that the encrypted string in the middle will change each time you run the code thanks to our initialization vector:
abc123K3gzWkxySUd6VkgvQTNJUUtZMjV2UT09Ojpia3sh1zglO3DYodw84855abc123
数组元素两两匹配
<?php$roomusers = [['0'=>[['userid' => 100, 'lev' => 0],['userid' => 55, 'lev' => 10],['userid' => 83, 'lev' => 1],['userid' => 22, 'lev' => 17],['userid' => 52, 'lev' => 18],['userid' => 53, 'lev' => 18],] // 0号房间的玩家]];// 循环所有房间,匹配每个房间的用户for ( $i = 0; $i < count( $roomusers ); $i ++ ) {while ( count( $roomusers[ $i ] ) > 1 ) {$r = matchusers( $roomusers[ $i ] );for ( $j = 0; $j < count( $roomusers[ $i ] ); $j ++ ) {if ( $roomusers[ $i ][ $j ]['userid'] == $r['userid']|| $roomusers[ $i ][ $j ]['userid'] == $r['touserid'] ) {$roomusers[ $i ][ $j ]}}$roomusers[ $i ] = array_values( $roomusers[ $i ] );echo json_encode( $r );// 发送匹配上的对方用户id给请求用户, 发请求用户的id给匹配上的用户echo json_encode( $roomusers[ $i ] ) . PHP_EOL;}}// 匹配某个房间的用户function matchusers($thisroomusers) {$length = count($thisroomusers);$result = ['userid' => 0, 'touserid' => 0, 'diff' => PHP_INT_MAX];for ($i=0; $i<$length; ++$i) {for ($j=$i+1; $j<$length; ++$j) {$diff = abs($thisroomusers[$i]['lev'] - $thisroomusers[$j]['lev']);if ($diff < $result['diff']) {$result['userid'] = $thisroomusers[$i]['userid'];$result['touserid'] = $thisroomusers[$j]['userid'];$result['diff'] = $diff;}}}return $result;}// 如果不是按索引顺序的,使用foreach$roomusers = ['2'=>[['userid' => 100, 'lev' => 0],['userid' => 55, 'lev' => 10],['userid' => 83, 'lev' => 1],['userid' => 22, 'lev' => 17],['userid' => 52, 'lev' => 18],['userid' => 53, 'lev' => 18],], // 二号房间的玩家'3'=>[['userid' => 11, 'lev' => 0],['userid' => 54, 'lev' => 10],['userid' => 45, 'lev' => 1],['userid' => 67, 'lev' => 17],['userid' => 23, 'lev' => 18],['userid' => 59, 'lev' => 18],],];foreach ($roomusers as $kk=>&$roomuser) {while (count($roomuser) > 1 ) {$r = matchusers($roomuser);foreach ($roomuser as $key => $user) {if ( $user['userid'] == $r['userid']|| $user['userid'] == $r['touserid'] ) {unset( $roomuser[ $key ] );}}$roomuser = array_values( $roomuser );echo $k;//房间号echo json_encode( $r ) . PHP_EOL;echo json_encode( $roomuser) . PHP_EOL;}}
剔除数组的重复元素
<?php$roomusers = ['3'=>[['userid' => 100, 'lev' => 0],['userid' => 55, 'lev' => 10],['userid' => 55, 'lev' => 1],['userid' => 22, 'lev' => 17],['userid' => 52, 'lev' => 18],['userid' => 53, 'lev' => 18],], // 二号房间的玩家'4'=>[['userid' => 12, 'lev' => 0],['userid' => 13, 'lev' => 10],['userid' => 14, 'lev' => 1],['userid' => 15, 'lev' => 17],['userid' => 18, 'lev' => 18],['userid' => 20, 'lev' => 18],],];// 剔除重复的玩家foreach ($roomusers as $key => $roomuser) {foreach ($roomuser as $k=>$v) {if (isset($_data[$key][$v['userid']])) {// found duplicatecontinue;}// remember unique item$_data[$key][$v['userid']] = ($v);}$_data[$key] = array_values($_data[$key]);$roomusers = ($_data);}print_r($roomusers);$data = array(array('md5' => 'alpha','some' => 'value',),array('md5' => 'alpha','some' => 'other value',),array('md5' => 'bravo','some' => 'third value',),);// walk input array$_data = array();foreach ($data as $v) {if (isset($_data[$v['md5']])) {// found duplicatecontinue;}// remember unique item$_data[$v['md5']] = $v;}// if you need a zero-based array, otheriwse work with $_data$data = array_values($_data);// 使用数组键的唯一性$arr = [['userid' => 100, 'lev' => 0],['userid' => 55, 'lev' => 10],['userid' => 55, 'lev' => 1],['userid' => 22, 'lev' => 17],['userid' => 52, 'lev' => 18],['userid' => 53, 'lev' => 18],];$index = [];foreach ($arr as $key=>$value) {if (isset($index[$value['userid']])) {unset($arr[$key]);}$index[$value['userid']] = true;}$arr = array_values($arr);print_r($arr);
php花括号
Note: There must not be any gap between { and $. Else, PHP interpreter won’t consider the string after $ as a variable
When to use curly braces:
When you are defining a variable inside a string, PHP might mix up the variable with other characters if using simple syntax to define a variable and this will produce an error. See the example below:
<?php$var = "way";echo "Two $vars to defining variable in a string.";?>
Output:
Notice: Undefined variable: vars …
In the above example, PHP’s interpreter considers $vars a variable, but, the variable is $var. To separate a variable name and the other characters inside a string, you can use curly braces. Now, see the above example using curly braces-
<?php$var = "way";echo "Two {$var}s to define a variable in a string.";?>
Output:
Two ways to define a variable in a string.
Source: http://schoolsofweb.com/php-curly-braces-how-and-when-to-use-it/
curl get https
<?phpfunction curl_get_contents($url){$ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);$data = curl_exec($ch);curl_close($ch);return $data;}$data = curl_get_contents('https://dncapi.feixiaohao.com/api/coin/volrank?page=1&pagesize=20&time_type=1&webp=1&tdsourcetag=s_pctim_aiomsg');$arr = json_decode($data, 1);$new = [];foreach ($arr['data'] as $key => $value) {if (in_array($value['code'], ['bitcoin', 'ethereum', 'eos', 'tron'])) {$new[] = $value;}}echo json_encode($new);
use 结构
// use is not a function, it's part of the Closure syntax.// It simply makes the specified variables of the outer scope available inside the closure.<?php$foo = 42;$bar = function () {// can't access $foo in hereecho $foo; // undefined variable};$baz = function () use ($foo) {// $foo is made available in here by use()echo $foo; // 42}For example:$array = array('foo', 'bar', 'baz');$prefix = uniqid();$array = array_map(function ($elem) use ($prefix) {return $prefix . $elem;}, $array);// $array = array('4b3403665fea6foo', '4b3403665fea6bar', '4b3403665fea6baz');
随机数
<?php// 5位随机数字 最多3位连续一样的数字function generate_random_numbers($exit_numbers){$array = range(00000, 99999);foreach ($array as $k=>$v) {// 过滤已发的牌照if (in_array($v ,$exit_numbers)) {$key2 = array_search($v, $array);unset($array[$key2]);}// 过滤最多3次重复数字if (preg_match("/(\d)\\1{3,}/",$v)) { // 回溯引用 '\1' 前面要加转义字符 '\\1'$key = array_search($v, $array);unset($array[$key]);}}shuffle($array);return isset($array[0]) ? sprintf("%'.05d\n", $array[0]) : 0;}
php list
$a = “a|b”;
list($c, $d) = explode(‘|’, $a);
echo $d; // b
抽奖
<?php/**** 油5 - 19** 油10 - 10** 油100 - 2** 算力1 - 2** 算力2 - 1** 道具1 - 33** 道具1 - 33**/class A{public function open_lottery(Request $request){$uuid = $request->param('uuid');$userid = $this->my_decrypt($uuid, $this->publickey);if (!$userid) {error("缺少关键信息");}$percent = rand(0,100);$res = $this->add_user_coin_notice($userid, 'GMS', 500, "抽奖消耗");if (!$res) {error("您的GMS币不够");}$data = [];if ($percent <= 19) {// 油+5\app\admin\model\UserItems::where('userid', $userid)->where('configsid', 14)->setInc('remain', 5);$data = ['name'=>'汽油', 'value'=>5, 'unit'=>'ml', 'configsid'=>14];} elseif ($percent <= 19+10) {// 油+10\app\admin\model\UserItems::where('userid', $userid)->where('configsid', 14)->setInc('remain', 10);$data = ['name'=>'汽油', 'value'=>10, 'unit'=>'ml', 'configsid'=>14];} elseif ($percent <= 19+10+2) {// 油+100\app\admin\model\UserItems::where('userid', $userid)->where('configsid', 14)->setInc('remain', 100);$data = ['name'=>'汽油', 'value'=>100, 'unit'=>'ml', 'configsid'=>14];} elseif ($percent <= 19+10+2+2) {// 算力+1\app\admin\model\Users::where('userid', $userid)->setInc('power');$data = ['name'=>'算力', 'value'=>1, 'unit'=>'', 'configsid'=>-1];} elseif ($percent <= 19+10+2+2+1) {// 算力+2\app\admin\model\Users::where('userid', $userid)->setInc('power', 2);$data = ['name'=>'算力', 'value'=>2, 'unit'=>'', 'configsid'=>-1];} elseif ($percent <= 19+10+2+2+1+33) {// 道具+1$this->gain_configs($userid, 1, '抽奖奖励');$data = ['name'=>'道具', 'value'=>1, 'unit'=>'个', 'configsid'=>0];} elseif ($percent <= 19+10+2+2+1+33+33) {// 道具+1$this->gain_configs($userid, 1, '抽奖奖励');$data = ['name'=>'道具', 'value'=>1, 'unit'=>'个', 'configsid'=>0];}success("success", $data);}}// 方法一$priorities = array(6=> 10,5=> 40,4=> 35,3=> 5,2=> 5);# you put each of the values N times, based on N being the probability# each occurrence of the number in the array is a chance it will get picked up# same is with lotteries$numbers = array();foreach($priorities as $k=>$v){for($i=0; $i<$v; $i++)$numbers[] = $k;}# then you just pick a random value from the array# the more occurrences, the more chances, and the occurrences are based on "priority"$entry = $numbers[array_rand($numbers)];echo "x: ".$entry;// 方法二Create a number between 1 and 100.If it's <= 10 -> 6Else if it's <= 10+40 -> 5Else if it's <= 10+40+35 -> 4And so on...
function shownum($num){
$rnum=number_format($num,10,’.’,’’);
$rnum=trim($rnum,’0’);
if(substr($rnum,0,1)==’.’)$rnum=’0’.$rnum;
if(substr($rnum,-1,1)==’.’)$rnum=substr($rnum,0,-1);
return $rnum;
}
nginx 404
laravel下 访问url404
解决:
如果你使用的是Nginx,使用如下站点配置指令就可以支持URL美化:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
