2014-12-25 115 views
1

我有一個函數,我正在使用自定義加密方法的實驗。該函數應該加密並返回值,但不返回任何內容。下面是函數:功能不正確返回值

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("https://stackoverflow.com/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("https://stackoverflow.com/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;

沒有。沒有錯誤,但沒有輸出。

編輯:然而,接受的答案是正確的,但是,更好地解釋我需要做的事情是在類的外部包含鍵並設置全局,然後在類中設置全局,正確。謝謝。

+0

你檢查過echo $ output函數嗎? –

+0

@GaneshKamath是的,我只是試過了,沒有運氣。 – Core

+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("https://stackoverflow.com/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^FA?2c_d%E^f7db0

Sory,我的英語不太好。