Jump to content

preg_match_all with PREG_SET_ORDER ignoring potential match?


Boozi

Recommended Posts

Hi all,

Long time reader, first time poster...

I've hit a wall trying to get this working : I'm trying to extract sets of fields from a non-static file. The fields inside this file are defined like this :
[code]

[[fieldname]]content of field.[[/fieldname]]

[/code]

Opening/closing tags & content can be spread across multiple lines like so:
[code]

[[field1]]content1[[/field1]]

[[field2]]
content2
[[/field2]]

[[field3]]
line1
line2
line3
[[/field3]]

[/code]

This is what I have so far:
[code]

preg_match_all( '/\[\[([a-z0-9]+)\]\](.*)\[\[\/([a-z0-9]+)\]\]/si' , $filestring , $fields , PREG_SET_ORDER );

[/code]

The resulting $fields contains this : (spaces added for extra readability)
[code]

    [0] => Array
        (
            [0] => [[field1]]content1[[/field1]]

                      [[field2]]
                      content2
                      [[/field2]]

                      [[field3]]
                      line1
                      line2
                      line3
                      [[/field3]]
            [1] => field1
            [2] => content1[[/field1]]

                      [[field2]]
                      content2
                      [[/field2]]

                      [[field3]]
                      line1
                      line2
                      line3

            [3] => field3
        )

[/code]

It seems that preg_match_all "skips" [[/field1]] and [[/field2]], am I overlooking something obvious?

The result I'm looking for is this : (spaces added for extra readability)
[code]

    [0] => Array
        (
            [0] => [[field1]]content1[[/field1]]
            [1] => field1
            [2] => content1
            [3] => field1
        )

    [1] => Array
        (
            [0] => [[field2]]
                      content2
                      [[/field2]]
            [1] => field2
            [2] => content2
            [3] => field2
        )

    [2] => Array
        (
            [0] => [[field3]]
                      line1
                      line2
                      line3
                      [[/field3]]
            [1] => field3
            [2] => line1
                      line2
                      line3
            [3] => field3
        )

[/code]

Any ideas or solutions would be much appreciated!
Link to comment
Share on other sites

[code]'/\[\[([a-z0-9]+)\]\](.*)\[\[\/([a-z0-9]+)\]\]/si'[/code]

In your first [tt]([a-z0-9]+)[/tt] you've told it to capture the field name, but in the second [tt]([a-z0-9]+)[/tt], you haven't told it to match the [b]same[/b] field name. Replace the second portion with [tt]\1[/tt].
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.