为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕仙森 2022-09-25 20:20:55
我在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;}任何人都可以帮我吗?
查看完整描述

1 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

欢迎来到堆栈溢出,阿比德!


您能否添加当前输出和预期输出,也许还可以添加错误消息?因为我看到你使用调用时间传递引用,这在5.4中被删除了。().update_comment(&$comment)


我试图运行代码并采用它,我看到它在更改后似乎工作正常。下面是一个声明性和命令式方法的示例。


function update_comments($comments)

{

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

        foreach ($comments as $idx => $comment) {

            $comments[$idx] = update_comment($comment);

        }

    }


    return $comments;

}

并在这里作为声明性示例


function update_comments($comments)

{

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

        $comments = array_map(function ($comment) {

            return update_comment($comment);

        }, $comments);

    }


    return $comments;

}

下面是我如何使用您的代码的示例。您还可以在这里使用特定的PHP版本进行修补:http://sandbox.onlinephpfunctions.com/code/10e616237c0c75c8e3520bdf5866040231879cde


$comments = update_comments([

    ['comment' => 'This is my #1 comment.'],

    ['comment' => 'This is my #2 comment.']

]);


print_r($comments);


// Array

// (

//     [0] => Array (

//             [comment] => This is my <a href="#">#1</a> comment.

//         )

// 

//     [1] => Array (

//             [comment] => This is my <a href="#">#2</a> comment.

//         )

// 

// )

如果这有帮助,或者您的问题仍然存在,请告诉我。preg_replace()


查看完整回答
反对 回复 2022-09-25
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信