函数未正确返回值

时间:2014-12-25 17:19:08

标签: php

我有一个函数,我用于自定义加密方法的实验。该函数应加密并返回值,但不返回任何内容。这是功能:

public function encrypt($value, $strlength){
    include('include/scripts/keys.php');
    global $Keys;   
    $output = preg_replace("/Password/", $Keys['Password'], $value);
    $output = preg_replace("/password/", $Keys['password'], $output);
    $output = preg_replace("/PASSWORD/", $Keys['password'], $output);
    $output = preg_replace("/a/", $Keys['a'], $output);
    $output = preg_replace("/b/", $Keys['b'], $output);
    $output = preg_replace("/c/", $Keys['c'], $output);
    $output = preg_replace("/d/", $Keys['d'], $output);
    $output = preg_replace("/e/", $Keys['e'], $output);
    $output = preg_replace("/f/", $Keys['f'], $output);
    $output = preg_replace("/g/", $Keys['g'], $output);
    $output = preg_replace("/h/", $Keys['h'], $output);
    $output = preg_replace("/i/", $Keys['i'], $output);
    $output = preg_replace("/j/", $Keys['j'], $output);
    $output = preg_replace("/k/", $Keys['k'], $output);
    $output = preg_replace("/l/", $Keys['l'], $output);
    $output = preg_replace("/m/", $Keys['m'], $output);
    $output = preg_replace("/n/", $Keys['n'], $output);
    $output = preg_replace("/o/", $Keys['o'], $output);
    $output = preg_replace("/p/", $Keys['p'], $output);
    $output = preg_replace("/q/", $Keys['q'], $output);
    $output = preg_replace("/r/", $Keys['r'], $output);
    $output = preg_replace("/s/", $Keys['s'], $output);
    $output = preg_replace("/t/", $Keys['t'], $output);
    $output = preg_replace("/u/", $Keys['u'], $output);
    $output = preg_replace("/v/", $Keys['v'], $output);
    $output = preg_replace("/w/", $Keys['w'], $output);
    $output = preg_replace("/x/", $Keys['x'], $output);
    $output = preg_replace("/y/", $Keys['y'], $output);
    $output = preg_replace("/z/", $Keys['z'], $output);
    $output = preg_replace("/1/", $Keys['1'], $output);
    $output = preg_replace("/2/", $Keys['2'], $output);
    $output = preg_replace("/3/", $Keys['3'], $output);
    $output = preg_replace("/4/", $Keys['4'], $output);
    $output = preg_replace("/5/", $Keys['5'], $output);
    $output = preg_replace("/6/", $Keys['6'], $output);
    $output = preg_replace("/7/", $Keys['7'], $output);
    $output = preg_replace("/8/", $Keys['8'], $output);
    $output = preg_replace("/9/", $Keys['9'], $output);
    $output = preg_replace("/0/", $Keys['0'], $output);
    $output = preg_replace("/_/", $Keys['_'], $output);
    $output = preg_replace("/-/", $Keys['-'], $output);
    $output = preg_replace("/A/", $Keys['a'], $output);
    $output = preg_replace("/B/", $Keys['b'], $output);
    $output = preg_replace("/C/", $Keys['c'], $output);
    $output = preg_replace("/D/", $Keys['d'], $output);
    $output = preg_replace("/E/", $Keys['e'], $output);
    $output = preg_replace("/F/", $Keys['f'], $output);
    $output = preg_replace("/G/", $Keys['g'], $output);
    $output = preg_replace("/H/", $Keys['h'], $output);
    $output = preg_replace("/I/", $Keys['i'], $output);
    $output = preg_replace("/J/", $Keys['j'], $output);
    $output = preg_replace("/K/", $Keys['k'], $output);
    $output = preg_replace("/L/", $Keys['l'], $output);
    $output = preg_replace("/M/", $Keys['m'], $output);
    $output = preg_replace("/N/", $Keys['n'], $output);
    $output = preg_replace("/O/", $Keys['o'], $output);
    $output = preg_replace("/P/", $Keys['p'], $output);
    $output = preg_replace("/Q/", $Keys['q'], $output);
    $output = preg_replace("/R/", $Keys['r'], $output);
    $output = preg_replace("/S/", $Keys['s'], $output);
    $output = preg_replace("/T/", $Keys['t'], $output);
    $output = preg_replace("/U/", $Keys['u'], $output);
    $output = preg_replace("/V/", $Keys['v'], $output);
    $output = preg_replace("/W/", $Keys['w'], $output);
    $output = preg_replace("/X/", $Keys['x'], $output);
    $output = preg_replace("/Y/", $Keys['y'], $output);
    $output = preg_replace("/Z/", $Keys['z'], $output);
    $output = preg_replace("/ /", $Keys[' '], $output);
    $output = substr($output, 0, $strlength);
    return $output;     
}

更改每个值后,它应该返回输出,但是当我调用它时

$encrypted = $this->encrypt($value, 40);

然后

echo $encrypted;

没有返回任何内容。没有错误,但没有输出。

编辑:下面接受的答案是正确的,但是,为了更好地解释我需要做的是在类外部包含键并在那里设置全局,然后在类,它工作正常。谢谢。

1 个答案:

答案 0 :(得分:0)

您的功能输出长度控制。

//include('include/scripts/keys.php');

这包括文件数组示例

$Keys = array ('a' =>'a?2c7d', 'b' =>'b0*/&d', 'c' =>'c_d%e^f', 'Password'=> 'pass');

示例功能

function encrypt($value, $strlength){
    global $Keys; 
    $output = preg_replace("/Password/", $Keys['Password'], $value);
    $output = preg_replace("/a/", $Keys['a'], $output);
    $output = preg_replace("/b/", $Keys['b'], $output);
    $output = preg_replace("/c/", $Keys['c'], $output);

        return substr($output, 0, $strlength);

}

 echo encrypt('abc abc', 40);

返回?2c_d%e ^ f7db0 * /& dc_d%e ^ f a?2c_d%e ^ f7db0

Sory,我的英语不太好。