php8 的 preg_replace_callback - 致命错误

问题描述 投票:0回答:1

由于 php8 不再支持

create_function
,如何更新此代码以避免致命错误?

$str = @preg_replace_callback(
      '/>(\s*$\s*)</m',
      create_function('$matches', "return strpos('$matches[0]', ' ') === false?'><':'> <';"),
      $str
);

我尝试过这个,但似乎不起作用。不会抛出任何错误。看出有什么明显的问题了吗?

 $str = @preg_replace_callback(
       '/>(\s*$\s*)</m',
       function($matches) { return strpos('$matches[0]', ' ') === false?'><':'> <'; },
       $str
 );
php preg-replace-callback
1个回答
-1
投票

您的问题重复,答案确实存在这里 但是你的代码重写了:

$str = @preg_replace_callback(
      '/>(\s*$\s*)</m',
      fn($matches) => strpos($matches[0], ' ') === false?'><':'> <',
      $str
);
© www.soinside.com 2019 - 2024. All rights reserved.