Spoiler tags

Discussion in 'The Edge of the Forum' started by nloding, May 25, 2008.

  1. nloding
    OP

    nloding GBAtemp Fan

    Member
    400
    0
    Oct 22, 2006
    United States
    Grand Rapids, MI, USA
    So how do they do it? Why do I want to know?

    Warning: Spoilers inside!

    Honestly, I'm just posting this because I want to see the code behind it [​IMG]
     
  2. Narin

    Narin The Cheat Master, kupo!

    Former Staff
    2,624
    24
    Feb 19, 2008
    United States
    Warning: Spoilers inside!
     
  3. Austinz

    Austinz GBAtemp Regular

    Member
    118
    0
    Sep 2, 2007
    Joke topic!
     
  4. nloding
    OP

    nloding GBAtemp Fan

    Member
    400
    0
    Oct 22, 2006
    United States
    Grand Rapids, MI, USA
    Not a joke, I'll post how they work.

    Regex: "/\[spoiler\]([\s\S]*)\[\/spoiler\]/'

    That regex is run, probably with preg_replace_callback. The function it calls takes the match between the () and does the following:

    Creates a container div with the overall styling and the heading that says WARNING. The (show) and (hide) links are contained in div's with an inline display setting. Both have onClick functions. There is then a div with the actual spoiler text. If 'show' is clicked, it sets it's display to 'none', hide's to 'inline' and the spoiler to 'block'. The reverse happens with the hide click.

    The callback function also assigns a unique number to the div's so that multiple spoilers can be used per page.

    Fun shtuff, Wordpress plugin coming soon!

    (Why there isn't a decent WP plugin that does this already, I don't know)
     
  5. Narin

    Narin The Cheat Master, kupo!

    Former Staff
    2,624
    24
    Feb 19, 2008
    United States
    Regex? Especially the one you posted is so efficent. What about nested tags or the use of the title bbcdode to specify a title for the spolier?
     
  6. nloding
    OP

    nloding GBAtemp Fan

    Member
    400
    0
    Oct 22, 2006
    United States
    Grand Rapids, MI, USA
    I didn't know you could post a title. That'd be kinda cool ... the regex change would be simple:

    preg_match_all('/\[spoiler(\s*title="([^"]*)")?\]([\s\S]*)\[\/spoiler\]/i', $input, $matches);

    That would make $matches[2] the title, and $matches[3] the spoiler itself.
     
  7. Narin

    Narin The Cheat Master, kupo!

    Former Staff
    2,624
    24
    Feb 19, 2008
    United States
    Why not just use pointers and string matches? They tend to work a lot more efficiently than using regex in such cases like this.
     
  8. nloding
    OP

    nloding GBAtemp Fan

    Member
    400
    0
    Oct 22, 2006
    United States
    Grand Rapids, MI, USA
    The advantage is the preg_replace_callback function PHP provides. If there's 2 spoilers per post on a single page, that one helluva a lot of string matching. Or ... you can just do preg_replace_callback($pattern 'function_name', $matches) and it does it all in one shot.

    I don't think regex is that inefficient, and it allows for a significantly greater range of flexibility.