There is a function to translate BBCode to HTML entities (PHP):

$patterns = array( '#\[b\](.*?)\[/b\]#', '#\[u\](.*?)\[/u\]#', '#\[strike\](.*?)\[/strike\]#', '#\[em\](.*?)\[/em\]#', ); $replacements = array( '<b>$1</b>', '<u>$1</u>', '<strike>$1</strike>', '<em>$1</em>', ); $text = preg_replace($patterns, $replacements, $text); 

How can I make the finished text processed using this function, when hit in <textarea> , changed back to BBCode?
That is, from lol <b>text 123</b> to the text engine should get lol [b]text123[/b] .

thank

    1 answer 1

    I would just use a regular expression and replace it with a pattern: /<(/?b)>/ig => "[$1]" If not mistaken, then so.

    Example:

     var str = "<b>azaza</b> aza <b>kek</b> lool <B></B> dcs"; var n_str = str.replace(/<(\/?b)>/ig, "[$1]"); 

    i at the end of the pattern is any case of letters: b, b. If only b is needed, i need to be removed. g at ​​the end means that you need to find not the first match, but all matches in the string.

    • but if you can, it’s not difficult for you to compile the full code of the function, I just don’t know about it - Vladimir
    • Thanks, your method came up to me, but could you tell me how to make an array of this? but I also have <u> => [u] and so on, and I have to do a few primitive lines - Vladimir
    • and also, I completely forgot, make it possible to capture the text if there are peresons, line breaks, etc., but now the regular only works on the line, thanks - Vladimir
    • I did not understand about the array, i.e. create an array of strings in each cell which will be the text between the tags or how? That worked not for one line: /<(\/?b)>/igm - Max
    • I mean something like this: <b> <em> <u> replace with [u | em | b] to shorten the line in general - Vladimir