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
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;
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.