문제

I have a preg_replace_callback which takes a closure (anonymous) function as 2nd parameter and it works perfectly fine on local, but when I deploy it to live environment it results in error => Internal server error 500. When i remove the Closure it works.

$regExPattern = '/\<%(?<content>.*?)%\>/';
$template = preg_replace_callback($regExPattern, function ($matches)  use ($dataItem) {
    if(isset($dataItem[trim($matches['content'])])) {
        return $dataItem[trim($matches['content'])];
    }
    else {
        return '';
    }
}, $template);

Any suggestions how can i work arround this problem. I need to use $dataItem inside my callback function and pass it to preg_replace_callback. My development environment is code igniter.

도움이 되었습니까?

해결책

Anonymous functions only work in PHP 5.3 and up. You could use create_function() instead:

$regExPattern = '/\<%(?<content>.*?)%\>/';
$template = preg_replace_callback($regExPattern, create_function(
      '$matches'
    , 'if(isset($dataItem[trim($matches[\'content\'])])) {
          return $dataItem[trim($matches[\'content\'])];
      }
      else {
          return "";
      }'
    )
);

Untested, of course.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top