2014-12-25 5 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 $ 출력을 함수 내부에서 확인 했습니까? –

+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 % 전자^f7db0 */& dc_d %의 전자^FA? 2c_d % 전자^f7db0 파일 어레이의 예를 포함

Sory, 내 영어가 좋지 않습니다.

관련 문제