Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

array_walk 造成系统崩溃

问题描述

这是一个简单的手机加密的方法,在循环里调用,但是1千不到的数据就直接504崩溃了,不知道是什么原因,求大神赐教!

问题出现的环境背景及自己尝试过哪些方法

在循环里面处理电话号码加密,如果不用array_walk 就不会崩溃

相关代码

protected function replacePhone(&...$phone){
     if ($this->getFlag()) {
         array\_walk($phone, function (&$item) {
            $item = mb\_substr\_replace($item, '****', 3, 4);
         }); 
     }
 }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

问题描述不清楚

  1. $phone是什么类型?数组?如果是一维数组,使用...不就展开了吗?不能再使用array_walk
  2. mb_substr_replace这个函数不是自带函数啊,应该附上代码
$phone = [];
foreach (range(1, 5000) as $number) {
    $phone[] = '1591234'.str_repeat('0',4-strlen($number)).$number;
}

replacePhone($phone);

foreach ($phone as $number) {
    echo $number;
    echo PHP_EOL;
}

function replacePhone(&$phone){
     array_walk($phone, function (&$item) {
            $item = mb_substr_replace($item, '****', 3, 4);
     }); 
}
 
function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = "UTF-8") {
    if (extension_loaded('mbstring') === true){
        $string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding);
        if ($start < 0) { $start = max(0, $string_length + $start); }
        else if ($start > $string_length) {$start = $string_length; }
        if ($length < 0){ $length = max(0, $string_length - $start + $length);  }
        else if ((is_null($length) === true) || ($length > $string_length)) { $length = $string_length; }
        if (($start + $length) > $string_length){$length = $string_length - $start;} 
        if (is_null($encoding) === true) {  return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length); }
        return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding);
    }
    return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...