preg_replace within the preg_replace

前端 未结 2 1016
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 06:39

Right now I\'m having issues replacing strings that already come out from preg_match. Lets say I have bbcode of [b]bla[/b], I have this part working with replacing

2条回答
  •  有刺的猬
    2021-01-27 07:08

    You could do something like this:

    $string = '[b]hi [b]test[/b][/b]';    
    do {
        $string = preg_replace('/\[b\](.*)\[\/b\]/', '$1', $string, -1, $count);
    } while ($count > 0);
    

    Or just use @Justinas' idea (from your OT's comment) if it's OK to replace all [b] with and [/b] with (regardless of them being in the right order/as pairs).

    Edit: you also need to change your quote regex to this:

    /\[quote(?:=(\d+))?\](.*)\[\/quote\]/s

    s flag allows . to match newlines (you probably want to add it to the other ones too). I also fixed the quote ID capturing part.

提交回复
热议问题