亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號