Jump to content

Replacing bbcode help?


Tenaciousmug

Recommended Posts

I'm trying to code a function that strips all BBCodes.. but I'm having trouble with the color since it has =whatever they type... This is how it looks:

    public function strip_bbcode($string) {
        // All the default bbcode arrays.
            $bbcode = array(
                    //Text Apperence
                    '
[center]' => '',
                    '[/center]
' => '',
                    '[b]' => '',
                    '[/b]' => '',
                    '[i]' => '',
                    '[/i]' => '',
                    '[u]' => '',
                    '[/u]' => '',
                    '[s]' => '',
                    '[/s]' => '',
                    '
[right]' => '',
                    '[/right]
' => '',
                    '
[left]' => '',
                    '[/left]
' => '',
                    '[small]' => '',
                    '[/small]' => '',
                    '[normal]' => '',
                    '[/normal]' => '',
                    '[big]' => '',
                    '[/big]' => '',
                    '[large]' => '',
                    '[/large]' => '',
                    '[color=(.*?)]' => '',
                    '[url=(.*?)\](.*?)\[/url]#si' => '\\2',
                    '[img]' => '',
                    '[/img]' => ''
            );
            $output = str_replace(array_keys($bbcode), array_values($bbcode), $string);
            return $output;
    } 

 

Anyone have any idea on how to do the color and the url?? I just basically want to replace it with emptiness.

Link to comment
https://forums.phpfreaks.com/topic/263098-replacing-bbcode-help/
Share on other sites

Figured it out. If anyone is curious, here is the solution:

    //removes bbcode
    public function strip_bbcode($string) {
        // All the default bbcode arrays.
            $bbcode = array(
                    //Text Apperence
                    '#\[b\](.*?)\[/b\]#si' => '\\1',
                    '#\[i\](.*?)\[/i\]#si' => '\\1',
                    '#\[u\](.*?)\[/u\]#si' => '\\1',
                    '#\[s\](.*?)\[/s\]#si' => '\\1',
                    '#\[center\](.*?)\[/center\]#si' => '\\1',
                    '#\[right\](.*?)\[/right\]#si' => '\\1',
                    '#\[left\](.*?)\[/left\]#si' => '\\1',
                    //Font Color & Size
                    '#\[color=(.*?)\](.*?)\[/color\]#si' => '\\2',
                    '#\[small\](.*?)\[/small\]#si' => '\\1',
                    '#\[normal\](.*?)\[/normal\]#si' => '\\1',
                    '#\[big\](.*?)\[/big\]#si' => '\\1',
                    '#\[large\](.*?)\[/large\]#si' => '\\1',
                    //Text Effects
                    '#\[bl\](.*?)\[/bl\]#si' => '\\1',
                    '#\[marquee\](.*?)\[/marquee\]#si' => '\\1',
                    //Other
                    '#\[url=(.*?)\](.*?)\[/url]#si' => '\\2',
                    '#\[quote\](.*?)\[/quote\]#si' => '\\1',
                    '#\[img\](.*?)\[/img\]#si' => '\\1'
            );
            $output = preg_replace(array_keys($bbcode), array_values($bbcode), $string);
            return $output;
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.