1. nloding

    OP nloding GBAtemp Fan
    Member

    Joined:
    Oct 22, 2006
    Messages:
    400
    Country:
    United States
    So how do they do it? Why do I want to know?

    Because I'm trying to build a Wordpress plugin that does this same thing!

    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

    Joined:
    Feb 19, 2008
    Messages:
    2,629
    Country:
    United States
    [title:Do you really wanna know]I don't know
     
  3. Austinz

    Austinz GBAtemp Regular
    Member

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

    OP nloding GBAtemp Fan
    Member

    Joined:
    Oct 22, 2006
    Messages:
    400
    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

    Narin The Cheat Master, kupo!
    Former Staff

    Joined:
    Feb 19, 2008
    Messages:
    2,629
    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 nloding GBAtemp Fan
    Member

    Joined:
    Oct 22, 2006
    Messages:
    400
    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

    Narin The Cheat Master, kupo!
    Former Staff

    Joined:
    Feb 19, 2008
    Messages:
    2,629
    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 nloding GBAtemp Fan
    Member

    Joined:
    Oct 22, 2006
    Messages:
    400
    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.
     
Draft saved Draft deleted
Loading...

Hide similar threads Similar threads with keywords - Spoiler,