Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    198
    Thanks
    78
    Thanked 5 Times in 5 Posts

    Strip Certain BBcode from Post Regex

    Hi guys I wanted to make a function to remove certain bbcode from my user comments only problem is I don't know much regex code.
    PHP Code:
    <?php
    function stripBBCode($text$code$replace '') {
        
    $pattern "/\[?.($code)\]/";
        return 
    preg_replace($pattern$replace$text);
    }
    $text "[url=http://google.com]google[/url] is fine [img]http://www.google.com/image/image.jpg[/img]";

    echo 
    stripBBCode($text'(url||img)');
    ?>
    The basic idea is to supply the bbcode I want to remove to my function.
    And then use regex and preg_replace to clean the text.
    Now the idea is to keep the text between the bbcode so users can still for example go or see the image if the really wanted to.
    So for example if I have this text:

    PHP Code:
    [url=http://google.com]google[/url] is fine [img]http://www.google.com/image/image.jpg[/img] 
    And I run the code I want it to become:
    PHP Code:
    google is fine http://www.google.com/image/image.jpg 
    Last edited by conware; 07-26-2012 at 05:07 PM.

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    239
    Thanks
    1
    Thanked 56 Times in 55 Posts
    PHP Code:
    function stripBBCode($text$code)
    {
            
    $code str_replace(",""|"$code);
            
    $code str_replace(" """$code);
            
            
    /**
             * replace the text that starts with a [ 
             * followed by an optional /
             * followed by any provided bbcode (case-insensitive, non-capturing group)
             * followed by 0 or more characters that are not ]
             * and ends with ]
             **/
            
    $pattern '#\[/?(?:' $code ')[^]]*\]#i';   

            return 
    preg_replace($pattern''$text); 
    }

    $text "[url=http://google.com]google[/url] is fine [img]http://www.google.com/image/image.jpg[/img]"

    echo 
    stripBBCode($text"url,img"); 

  • Users who have thanked gvre for this post:

    conware (07-26-2012)

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    198
    Thanks
    78
    Thanked 5 Times in 5 Posts
    Thanks gvre, you helped me out alot


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •