psunshine Posted July 2, 2008 Share Posted July 2, 2008 Hi, Maybe this cant be done with regular expressions or maybe I am a complete newbie which is why I am posting here I have a list of items, each on newlines that can look like this: <item><1>abc123</1><2>abc123</2><3>abc123</3><4>abc123</4></item> <item><1>abc123</1><3>abc123</3><5>abc123</5><6>abc123</6></item> <item><1>abc123</1><6>abc123</6><2>abc123</2><3>abc123</3></item> I have used preg_match_all before to break each subpattern in the item in to an array which can then be inserted to database, but that has always been where the pattern in item follows the same structure for each line if (preg_match_all('/<item><1>(.+)</1><2>(.+)</2><3>(.+)</3><4>(.+)</4></item>/mi', $result, $matches)) where $result looks like this: <item><1>abc123</1><2>abc123</2><3>abc123</3><4>abc123</4></item> <item><1>abc123</1><2>abc123</2><3>abc123</3><4>abc123</4></item> <item><1>abc123</1><2>abc123</2><3>abc123</3><4>abc123</4></item> So how do I acheive the same when the pattern may or may not contain a certain pattern without missing any pattern? On the same note, is it possible to get $matches to return "none" for the lines which dont contain a pattern for a specific part of the item? For example I know that each item could have a possible 6 tags within but I dont which 6 it is and want to return "none" for the tags not included. I hope this makes sense and that someone can help. This has been driving me crazy trying to figure it out! Please let me know if it needs better explanation. Thanks Pat Quote Link to comment https://forums.phpfreaks.com/topic/112914-solved-preg_match_all-where-pattern-can-be-different/ Share on other sites More sharing options...
effigy Posted July 2, 2008 Share Posted July 2, 2008 <pre> <?php $lines = array( '<item><1>abc1</1><2>abc2</2><3>abc3</3><4>abc4</4></item>', '<item><1>abc1</1><3>abc3</3><5>abc5</5><6>abc6</6></item>', '<item><1>abc1</1><6>abc6</6><2>abc2</2><3>abc3</3></item>' ); $data = array(); $num = 1; foreach ($lines as $line) { preg_match_all('%<(\d+)>(.+?)</\1>%', $line, $matches); foreach ($matches[1] as $key => $value) { $data[$num][$value] = $matches[2][$key]; } ++$num; } foreach ($data as &$line) { for ($key = 1; $key <= 6; $key++) { if (!array_key_exists($key, $line)) { $line[$key] = 'none'; } } } print_r($data); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/112914-solved-preg_match_all-where-pattern-can-be-different/#findComment-580049 Share on other sites More sharing options...
psunshine Posted July 2, 2008 Author Share Posted July 2, 2008 WOW! You are an absolute god! It worked perfectly first time on over a thousand rows!!! So a thousand thank yous and thumbs up Quote Link to comment https://forums.phpfreaks.com/topic/112914-solved-preg_match_all-where-pattern-can-be-different/#findComment-580220 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.