Jump to content

[SOLVED] Preg_match_all where pattern can be different?


psunshine

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.