imperimus Posted March 1, 2015 Share Posted March 1, 2015 Hi Folks, Been getting into a little coding recently and trying to parse some templates that will need embedded objects found and processed in order to generate the output. I have the following string which I need to be bale to extract the parameters from in order to process the next part of the code. I have manage to do this by doing multiple preg_match_all, but it is not very efficient and/or dynamic. Example strings (the source might contain multiples): <==OBJECTSTART==>type=>sqltable,objectid=>4000001,options=>1|5|2<==OBJECTEND==> <==OBJECTSTART==>type=>sqltable,objectid=>4000002,options=>3|8|5<==OBJECTEND==> What I have so far I have go to the following for a regex expression: /<==OBJECTSTART==>((.?),)(.?)<==OBJECTEND==>/ This gives me the information before the first comma but I have tried the usual + and * to give me a repeat iteration but no luck. ideally I am looking for an array of objects that looks like the following [0]=> [type]=sqltable [objected]=4000001 [options]=1|5|2 [1]=> [type]=sqltable [objected]=4000002 [options]=3|8|5 thanks in advance! Link to comment https://forums.phpfreaks.com/topic/294995-create-multi-dimension-recursive-regex-with-php-pairs-array-from-preg_match_all/ Share on other sites More sharing options...
imperimus Posted March 2, 2015 Author Share Posted March 2, 2015 With a little help I have ended up with the following Function: function process_objects($data){ $pattern = "/<==OBJECTSTART==>.*?<==OBJECTEND==>/"; preg_match_all($pattern, $data, $objects); if(!empty($objects[0])){ foreach($objects as $objectarr){ foreach($objectarr as $object){ $line = preg_replace('/<==OBJECTSTART==>(.*)<==OBJECTEND==>/', '\1', $object); $pairs = explode(',', $line); $new_data = array(); foreach ($pairs as $pair) { list ($key, $value) = explode('=>', $pair); $new_data[$key] = $value; } $replacement = get_object($new_data[type],$new_data[objectid],$new_data[options]); $data = str_replace($object, $replacement, $data); } } // return $data; } } function get_object($objtype, $objid, $objoptions){ $item = "<table><tr><td>this is an object: ".$objtype."</td></tr></table>\n"; return $item; } Seems to work, but not sure if it is the most efficient way to get there! Link to comment https://forums.phpfreaks.com/topic/294995-create-multi-dimension-recursive-regex-with-php-pairs-array-from-preg_match_all/#findComment-1507304 Share on other sites More sharing options...
requinix Posted March 3, 2015 Share Posted March 3, 2015 There's no way to get one regex to do that because (1) can't match more than one thing in a collection of other things, and (2) can't get array keys like that. imperimus's solution is basically how you have to do it, though there's flexibility on exactly how of course: 1. Match all the OBJECTSTART to OBJECTEND "tags" and loop over each 2. Match all key=>value pairs inside each tag and loop over each 3. Add to an array Link to comment https://forums.phpfreaks.com/topic/294995-create-multi-dimension-recursive-regex-with-php-pairs-array-from-preg_match_all/#findComment-1507311 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.