regex to remove bbcode tags with atrributes

限于喜欢 提交于 2020-02-07 06:48:29

问题


I've got a number of bbcode tags that have phpbb attributes (5 digit value - assuming text color or something). They look like this in the text:

This is [b:31747]bold[/b:31747] text and so is [b:17171]this[/b:17171].

I cannot get a regex working that finds bracket+b+colon+any_combo_of_5_digits+end_bracket and lets me replace it with corresponding html. Using php's preg_replace() function, if it makes a difference.


回答1:


The regular expression you need is:

\[/?b:\d{5}]



回答2:


This would replace bold, underline and italic tags.

$new_text = preg_replace('~\[(/?[bui]):\d+\]~is', '<$1>', $text);
echo $new_text; // This is <b>bold</b> text and so is <b>this</b>.



回答3:


This should work with both opening and closing tags for any type of tag:

$string = preg_replace("/\[(\/?[a-zA-Z]+):[\d]{5}\]/is", "<$1>", $string);



回答4:


preg_replace("/\[\/?b:[0-9]*?\]/","","[b:17171]this[/b:17171]");

http://ideone.com/fDCZM



来源:https://stackoverflow.com/questions/5505798/regex-to-remove-bbcode-tags-with-atrributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!