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
    New Coder
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    34
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Stop [code] tag from parsing other BBCode

    Hello, I recently built a comment system and I added BBCode to it today but I ran into a problem. I want to have a [code] tag so I used preg_replace() with something similar to this:
    PHP Code:
    $message preg_replace("/(\[b\])(.+)(\[\/b\])/s""<b>$2</b>"$message); 
    The problem is, I also have smilies and I use str_replace() for those so the smilies (and other BBCode) will get parsed inside the [code] tag which I do not want. I don't want the user to be able to disable BBCode or smilies so is there a way for the [code] tag to be processed differently than the other tags and stop smilies and other BBCode from being parsed?

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 12 Times in 12 Posts
    You may need to separate chunks of the user's text into different strings depending on what BBCode is used where, and run the strings through the appropriate preg_replace().

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    So, what I think you want is a preg_replace etc. that doesn't work for information encased inside [code] tags?

    You'll probably need a recursive solution to this. You'll need to use strpos() to find an occurence of [ code] (based on an offset passed into the function), then preg_replace up to the code tag using substr(), then find [ /code] using an offset based on the strpos() of it's respective [ code], and run the function again, passing the new offset as the [ /code].

    Let me know how you get on, I'm here to help. Recursion isn't easy to get, and if you haven't encountered it before I'll be more than happy to help futher.


  •  

    Posting Permissions

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