Spoiler tags

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

May 25, 2008

Spoiler tags by nloding at 3:53 AM (1,070 Views / 0 Likes) 7 replies

  1. nloding
    OP

    Member nloding GBAtemp Fan

    Joined:
    Oct 22, 2006
    Messages:
    400
    Location:
    Grand Rapids, MI, USA
    Country:
    United States
    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

    Former Staff Narin The Cheat Master, kupo!

    Joined:
    Feb 19, 2008
    Messages:
    2,624
    Country:
    United States
    Warning: Spoilers inside!
     
  3. Austinz

    Member Austinz GBAtemp Regular

    Joined:
    Sep 2, 2007
    Messages:
    118
    Country:
    Australia
    Joke topic!
     
  4. nloding
    OP

    Member nloding GBAtemp Fan

    Joined:
    Oct 22, 2006
    Messages:
    400
    Location:
    Grand Rapids, MI, USA
    Country:
    United States
    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

    Former Staff Narin The Cheat Master, kupo!

    Joined:
    Feb 19, 2008
    Messages:
    2,624
    Country:
    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

    Member nloding GBAtemp Fan

    Joined:
    Oct 22, 2006
    Messages:
    400
    Location:
    Grand Rapids, MI, USA
    Country:
    United States
    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

    Former Staff Narin The Cheat Master, kupo!

    Joined:
    Feb 19, 2008
    Messages:
    2,624
    Country:
    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

    Member nloding GBAtemp Fan

    Joined:
    Oct 22, 2006
    Messages:
    400
    Location:
    Grand Rapids, MI, USA
    Country:
    United States
    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.
     

Share This Page