Combining 2 regex expressions

前端 未结 2 543
离开以前
离开以前 2021-01-28 22:59

I\'m looking to try and combine 2 regex expressions.

$test = preg_replace(\'/\\[.*?\\]/\', \'\', $test);   

I use this to remove any square bra

相关标签:
2条回答
  • 2021-01-28 23:36

    Simplest answer is to use an or:

    '/\[[^\]]*\]|[\[\]=]+/'
    

    Notice that I also changed the inner pattern in the first string. I'm not sure if the backslash is needed there.

    0 讨论(0)
  • 2021-01-28 23:40

    Use an alternation:

    $test = preg_replace('/\[.*?\]|[\[\]=]/', '', $test);
    

    The ordering is here important, so that at first matching brackets, and their content, are removed. This should work fine as long as there are no nested brackets.

    So, this expression will match either \[.*?\] OR [\[\]=]

    0 讨论(0)
提交回复
热议问题