lang/php

PHP Warning: preg_replace(): The /e modifier is no longer supported

C/H 2018. 7. 9. 08:30

Warning: preg_replace(): The /e modifier is no longer supported

PHP 5.x에서 PHP7으로 업그레이드 시 발생하는 가장 일반적인 문제들 중 하나.

well-documented issue in PHP manual PHP 매뉴얼에서 v5.5 부터 사용되지 않으며(deprecated) v7.0.0에는 지원되지 않은(unsupported)다고 문서에 명시되어 있지만, 과거 레거시 코드를 그대로 이용할 경우 문제가 발생한다.

PHP – How to fix the “Warning: preg_replace(): The /e modifier is no longer supported” error in PHP7 페이지에서 문제를 해결할 수 있는 3가지 방법을 설명한다.

문제 해결 방법

  1. Stick with the Plan.

    표준(계획)에 충실하라. preg_replace 함수 대신 preg_replace_callback 함수를 이용하라고 제시.

    $str = preg_replace("/([a-z]*)/e", "strtoupper('\\1')", $str);
    
    $str = preg_replace_callback(
        "/([a-z]*)/",
        function($matches){
            foreach($matches as $match){
                return strtoupper($match);
            }
        }, 
        $input
    );
    
  2. Trick the System.

    시스템 트릭이용. preg_replace_callback으로 코드를 다시 구현할 수 없는 다양한 시나리오가 존재할 경우 이를 해결할 수 있는 방법에 대해서 제안한다.

    • use() 를 이용하는 방법. preg_replace_callback('#search#', function($m) use($rs){ /* code */ }, $str);
    • /e 수정자를 실제 eval() 호출로 대체.

    We know, this is almost as bad as stealing… and yet it gets the job done, assuming you can use the eval() function (which is disabled by most providers for obvious security reasons).
    우리는 이것이 도둑질만큼이나 나쁘다는 것을 알고 있습니다. eval () 함수를 사용할 수 있다고 가정 할 때, 작업은 끝납니다. (명백한 보안상의 이유 때문에 대부분의 공급자가 사용할 수 없습니다).
  3. Dig it under the carpet.

    카펫 밑에 파 묻어라. 위 2가지 방법으로도 해결이 안 될경우는 그냥 경고를 숨겨라.
    /e 를 지원하고, E_DEPRECATED, E_STRICT 경고를 출력하는 PHP <= 5.4 에서 PHP 5.5 업그레이드시 유용한 방법이다.
    PHP 5.5.x 에서 E_WARNING 를 E_DEPRECATED | E_STRICT 로 변경.
    그 외 PHP_error.log를 기록하지 않는 방법도 있다. PHP – How to disable error log, display errors and error reporting programmatically

    However, it’s very important to understand that hiding your script errors is almost always the worst thing you can do: be sure to understand all the implications and potential consequences of what you’re doing before proceeding.
    그러나 스크립트 오류를 ​​숨기는 것은 거의 항상 최악입니다. 진행하기 전에 수행중인 작업의 모든 결과와 잠재적 인 결과를 이해해야합니다.


반응형