1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Regex/pregmatch question?

Discussion in 'XenForo Development Discussions' started by Tigratrus, May 13, 2011.

  1. Tigratrus

    Tigratrus Well-Known Member

    Can anyone help me nail down the syntax for a preg_match to pull the BBCode out of the raw post content in the DB field?

    It's gotta be pretty close to:

    preg_match('/\[img(=(.*?))?\](.*?)\[\/img\]/', $subject, $results);

    At least I *think* so, I'm not even vaguely a regexian ;).

    Any help would be greatly appreciated!

  2. Tigratrus

    Tigratrus Well-Known Member

    Wait... Suddenly remembered that Jaxel is doing this in XenPorta.

    preg_match('#\[img\](.+?)\[/img\]#i', $post['message'], $matches)

    Aha! :) Thx Jaxel!

  3. Jaxel

    Jaxel Well-Known Member

    Regular Expressions are BAD!
  4. SheepCow

    SheepCow Well-Known Member

    You might want to stick a U flag in there to make it ungreedy
  5. Jaxel

    Jaxel Well-Known Member

    The question mark in (.+?) makes it ungreedy.
    Darkimmortal likes this.
  6. SheepCow

    SheepCow Well-Known Member

    Oh yes, too early in the morning ;)
  7. sifuhall

    sifuhall Active Member

    Could I use this to replace multiple line breaks with a single line break?

    I'm thinking the match pattern would be something like:


    And the replacement would be


    Would this work?
  8. Jaxel

    Jaxel Well-Known Member

    I would use this:
    $post['message'] = preg_replace('#\n{2,}#', "\n", trim($post['message']));
    This would search for instances of 2 or more \n tags and replace it with \n... you could also use \r\n if you needed it, but on my forum \n works better.
  9. sifuhall

    sifuhall Active Member


Share This Page