Jump to content

Create Multi dimension recursive regex with php pairs / array from preg_match_all


Go to solution Solved by imperimus,

Recommended Posts

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!

 

  • Solution

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!

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

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.