Boozi Posted August 6, 2006 Share Posted August 6, 2006 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]]line1line2line3[[/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 https://forums.phpfreaks.com/topic/16723-preg_match_all-with-preg_set_order-ignoring-potential-match/ Share on other sites More sharing options...
effigy Posted August 6, 2006 Share Posted August 6, 2006 [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 https://forums.phpfreaks.com/topic/16723-preg_match_all-with-preg_set_order-ignoring-potential-match/#findComment-70341 Share on other sites More sharing options...
Boozi Posted August 8, 2006 Author Share Posted August 8, 2006 Yup, that's done the trick!Thanks!! Link to comment https://forums.phpfreaks.com/topic/16723-preg_match_all-with-preg_set_order-ignoring-potential-match/#findComment-71442 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.