Stupid Regex. Need help again.

Discussion in 'PHP' started by blueparukia, Jul 19, 2008.

  1. #1
    Doubt I'll ever be able to use regex properly.

    What I need is a regex that will match:

    Text
    And:
    Text
    And:
    Text
    And:
    Text


    I've tried all sorts of crap, but since I just can't wrap my head around regex, I doubt any of it was real regex.

    Now I am sick of saying regex.

    Thank you,

    Josh
     
    blueparukia, Jul 19, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    '~\[spoiler(?:[ ]?=[ ]?(["\'])?([^"\'\]]+)\1?)?\](.*?)\[/spoiler\]~si'
    
    PHP:

    Regex isn't that hard once you know the basics. Check: www.phpvideotutorials.com/regex/
     
    nico_swd, Jul 19, 2008 IP
    blueparukia likes this.
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Thanks. Halfway through the video now :D

    Still a little confused though, cause now nothing is showing up in the replace.

    I thought the title would come out as $1 and the inner text would be $2, but it doesn't seem so :S

    EDIT: Its $2 and $3. Got it.

    Thanks again
     
    blueparukia, Jul 19, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Just for clarification: The first group starts with a colon followed by a question mark :)?), which means this group won't be captured in the $matches array.
     
    nico_swd, Jul 19, 2008 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    Cool.

    And just curious (always wondered about regex, since you can't directly manipulate the "variables" afaik.

    Can you have it, so that if a group is empty, replace it with something else?
     
    blueparukia, Jul 19, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    I don't think I understand your question. Can you give me an example?
     
    nico_swd, Jul 19, 2008 IP
  7. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #7
    Say, using my example, if we have

    Test

    It outputs something like:

    Hello<br/>Test
    Code (markup):
    But if we just had
    Test

    The output would be:

    <br/>Test
    Code (markup):
    And say I wanted a default value - such as "Title".

    Then if I had
    Test

    it would output:

    
    Title<br/>Test
    
    Code (markup):
    So just a default for one of the matches really...
     
    blueparukia, Jul 19, 2008 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    I don't think that's possible to specify directly in the regular expression itself. But I guess you could do something like:
    
    $pattern = '~\[spoiler(?:[ ]?=[ ]?(["\'])?([^"\'\]]+)\1?)?\](.*?)\[/spoiler\]~si';
    $text = preg_replace_callback($pattern, '__callback_handler', $text);
    
    function __callback_handler($matches)
    {
    	return sprintf("%s<br />\n%s",
    		!empty($matches[2]) ? $matches[2] : 'Title',
    		!empty($matches[3]) ? $matches[3] : 'Default'
    	);
    }
    
    PHP:
     
    nico_swd, Jul 19, 2008 IP
  9. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #9
    Ahh, I ended it up doing a longer way - easier to understand, but may not be as efficient.

    I used a another preg_replace() to replace any one with no attribute with one that does have an attribute.

    So
    or
    become
    Thanks

    BP
     
    blueparukia, Jul 19, 2008 IP