Results 1 to 4 of 4

Thread: regex replacement, with any contents

  1. #1
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default regex replacement, with any contents

    I'm trying to understand regex more, so this is just an exploratory question.

    Let's assume (without going into any security issues, here), that I wanted to parse BB code as html.
    The format would be [tag]...[/tag] which would become <tag>...</tag>

    My question is how to keep the middle of that, the name of the tag, and replace it in the output.

    In other words, replace '[' followed by any letters/numbers, no spaces (assume no parameters for now) then ']' with '<', the middle bit (letters/numbers), then '>'.

    The main question is how you keep the unknown match portion (any letters/numbers) and put that same portion into the output.

    I hope the question is clear enough.

    Or a simpler question would be "find any pattern of number-letter-number-letter in the page, then duplicate that three times with hyphens between".
    On the page, for example, "1a1a" would become "1a1a-1a1a-1a1a"
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $html = preg_replace('/\\[(\\/?[a-zA-Z0-9]+?)\\]/', '<$1>', $bbcode);
    Code:
    $repeated = preg_replace('/(\\d\\w\\d\\w)/', '$1-$1-$1', $original);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That's easy, then. How is $1 defined, though? For the first example, you also have [ and ] as part of the search, which you would not (or might in some cases) want included in the replace. How is this determined?

    EDIT: Ok, found it as PHP.net:
    replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern.
    Thanks. Good to know.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Note that that's in single quotes: $1 isn't interpreted as a variable.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •