PHP/Regex: simple regex for bbcode [s] or [strike] fails to work

后端 未结 1 1363
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 11:30

For a silly bbcode parser I wanted to add two definitions into one, my original definition was this for preg_replace:

\'#\\[s\\](.*?)\\[/s\\]#si\', \'

        
相关标签:
1条回答
  • 2021-01-03 12:13

    The problem is that you added two new regex groups, (s|strike) in the opening tag and (s|strike) in the closing tag. So inside your resulting code you will get s or strike. You can fix that by simply using the correct group number, 2.

    Another way would be to make that new groups non-referencing, by adding a ?: to the beginning, but I guess the first solution is easier to understand:

    #\[(?:s|strike)\](.*?)\[/(?:s|strike)\]#si
    
    0 讨论(0)
提交回复
热议问题