Jump to content


Photo

preg_match_all with PREG_SET_ORDER ignoring potential match?


  • Please log in to reply
2 replies to this topic

#1 Boozi

Boozi
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 August 2006 - 05:57 PM

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 :

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


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

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

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

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


This is what I have so far:

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


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

    [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
        )


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)

    [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
        )


Any ideas or solutions would be much appreciated!


#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 August 2006 - 07:33 PM

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

In your first ([a-z0-9]+) you've told it to capture the field name, but in the second ([a-z0-9]+), you haven't told it to match the same field name. Replace the second portion with \1.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Boozi

Boozi
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 08 August 2006 - 10:09 PM

Yup, that's done the trick!

Thanks!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users