How to replace [color = "custom color here"] custom text here [/ color] with text here

I use it in my commenting system so that users can change the color of a comment, for example, a person writes this in textarea: [color = "red] is red text [/ color], and this is black text like this: This is red text, and this is black. Moreover, “this is red text” will be red.

    1 answer 1

    This is solved using regular expressions:

    $str = '[color="red"]это красный текст[/color]'; $patt = '~\[color="([^"]+)"\](.+?)\[/color\]~'; $repl = '<p style="color:$1">$2</p>'; $str = preg_replace($patt, $repl, $str); echo $str; 
    • Thanks, I will check - Mr Danil
    • $ message = str_replace (array_keys ($ arrEmoji), array_values ​​($ arrEmoji), $ message); $ patt = '~ [color = "([^"] +) "] (. +?) [/ color] ~'; $ repl = '<p style =" color: $ 1 "> $ 2 </ p>' ; $ message = str_replace ($ patt, $ repl, $ message); For some reason, your code does not work if you use it with str_replace - Mr Danil
    • And if without that, in general, robots - Mr Danil