preg_replace更新到 PHP 5.4 后无法正常工作

我在preg_replace()有问题,我相信在将我的主机PHP版本从5.3更新到5.4之后。


代码以前工作正常,但现在有问题:


function update_comments($comments)

{

  if (!empty($comments) && is_array($comments)) {

    foreach ($comments as &$comment)

      update_comment(&$comment);

  }


  return $comments;

}


function update_comment($comment) {

$repl = '<a href="#">$0</a>';

$comment['comment'] = preg_replace('~#(\d+)~', $repl, $comment['comment']);

return $comment;

}

任何人都可以帮我吗?


慕仙森
浏览 84回答 1
1回答

猛跑小猪

欢迎来到堆栈溢出,阿比德!您能否添加当前输出和预期输出,也许还可以添加错误消息?因为我看到你使用调用时间传递引用,这在5.4中被删除了。().update_comment(&$comment)我试图运行代码并采用它,我看到它在更改后似乎工作正常。下面是一个声明性和命令式方法的示例。function update_comments($comments){&nbsp; &nbsp; if (!empty($comments) && is_array($comments)) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($comments as $idx => $comment) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $comments[$idx] = update_comment($comment);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $comments;}并在这里作为声明性示例function update_comments($comments){&nbsp; &nbsp; if (!empty($comments) && is_array($comments)) {&nbsp; &nbsp; &nbsp; &nbsp; $comments = array_map(function ($comment) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return update_comment($comment);&nbsp; &nbsp; &nbsp; &nbsp; }, $comments);&nbsp; &nbsp; }&nbsp; &nbsp; return $comments;}下面是我如何使用您的代码的示例。您还可以在这里使用特定的PHP版本进行修补:http://sandbox.onlinephpfunctions.com/code/10e616237c0c75c8e3520bdf5866040231879cde$comments = update_comments([&nbsp; &nbsp; ['comment' => 'This is my #1 comment.'],&nbsp; &nbsp; ['comment' => 'This is my #2 comment.']]);print_r($comments);// Array// (//&nbsp; &nbsp; &nbsp;[0] => Array (//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[comment] => This is my <a href="#">#1</a> comment.//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)//&nbsp;//&nbsp; &nbsp; &nbsp;[1] => Array (//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[comment] => This is my <a href="#">#2</a> comment.//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)//&nbsp;// )如果这有帮助,或者您的问题仍然存在,请告诉我。preg_replace()
打开App,查看更多内容
随时随地看视频慕课网APP