逐行读取txt文件-PHP

<?php

/**
 * 取余
 * @param string $userId
 * @return int
 */
function getRemainder(string $userId)
{
//        return $remainder = $userId % 100; #使用userId取模获取,比如放量x%:$remainder < x
    $key = "Z1SbMB";
    // 对userId进行加盐(key),然后md5加密
    $md5Str = md5($key . $userId);
    // 这个地方取前6位进行转换,因为取全部,算出来太长
    $subStr = substr($md5Str, 0, 6);
    // 将16进制转换为10进制,再对100取余
    return hexdec($subStr) % 100;
}

$hit = 2; // 命中率
$count = 0;
$hit_count = 0;
$file = fopen("users.txt", "r");
while (!feof($file)) {
    $userId = fgets($file);
    $userId = str_replace("\r\n", "", $userId); // 去掉换行符
    $userId = str_replace("\"", "", $userId); // 去掉"
    $remainder = getRemainder($userId);
    if ($remainder < $hit) {
        echo $userId . ':' . $remainder . ",";
        echo "命中" . "\n";
        $hit_count++;
    } else {
        echo $userId . ':' . $remainder . ",";
        echo "丢失" . "\n";
    }
    $count++;
//    if ($count >= 100000) break;
}
fclose($file);
echo "hit_count" . ":" . $hit_count . "\n";
echo "count" . ":" . $count . "\n";
exit;


$hit = 1; // 命中率
//$userId = strval(2);
//$userId = strval(rand(2086846789, 2086946789));
$hit_count = 0;
for ($i = 0; $i < 100000; $i++) {
    $userId = strval(2086846789 + $i);
    $remainder = getRemainder($userId);
    if ($remainder < $hit) {
        echo $userId . ':' . $remainder . ",";
        echo "命中" . "\n";
        $hit_count++;
    } else {
//        echo "丢失" . "\n";
    }
}
echo "hit_count" . ":" . $hit_count . "\n";

// mysql
// conv(substr(md5(concat('Z1SbMB',0)),1,6),16,10)%100
// select id,conv(substr(md5(concat('Z1SbMB',id)),1,6),16,10)%100 as number from user where id = 2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值