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 2 of 2

Thread: BBCode

  1. #1
    New Coder
    Join Date
    Nov 2012
    Location
    United Kingdom
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post

    BBCode

    Right i'm messing with a lot of PHP code latley getting used to stuff i don't know and i can't seem to get this BBCode to work :L

    PHP Code:
    function bbcode($string)
    {
        
    $built "";
        if (
    preg_match ("'\[quote\=(.*?)\](.*?)\[/quote\]'is"$string))
        {
            
    $built .= "1: <span>\\1</span> 2: <span>\\2</span> ";
        }
        return 
    preg_replace"'\[quote\=(.*?)\](.*?)\[/quote\]'is"$built$string );

    }

    $content "[quote=Kyle][quote=Fred]Mwhaha[/quote]Blah blah blah[/quote]";

    echo 
    bbcode($content); 
    Want it to quote as many quotes that are in each other.
    I tried while() but that didn't work either...
    Last edited by Clawed; 03-12-2013 at 02:14 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You need to write the recursive functionality for it. Both in the pattern itself, and recursively for the callback. So you'll want to be using preg_replace_callback instead.
    PHP Code:
    function bbCodeQuote($in)
    {
        
    $sPattern '/\[quote=(.+?)\]((?:(?R)|.*?)+)\[\/quote\]/i';
        if (
    is_array($in))
        {
            
    // this is at least one match.
            
    $in "<blockquote>Quoted By: {$in[1]}<br/>{$in[2]}</blockquote>";
        }
        return 
    preg_replace_callback($sPattern__FUNCTION__$in);
    }

    $content "[quote=Kyle][quote=Fred]Mwhaha[/quote]Blah blah blah[/quote]";

    print 
    bbCodeQuote($content); 
    So with the above that should result in:
    Code:
    <blockquote>Quoted By: Kyle<br/><blockquote>Quoted By: Fred<br/>Mwhaha</blockquote>Blah blah blah</blockquote>
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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