Jump to content

collecting information from an array


jay3ld

Recommended Posts

I have this file I can't parse because there would be lots of errors and undefined errors.

So I am using regex to pull out the array information

 

I have a basic array like this lets say:

$my_array = array(
    'key' => "value",
    'another_key' => 'another_value'
);

 

So far I have accomplished finding the first part of the array with:

preg_match_all("~\\\$([a-zA-Z0-9]+) = array\(([\s]+)('([a-zA-Z0-9]+)' => \"(.+?)\"(,))+[\s]+~", $contents, $matches);

 

The problem is I can't figure out how to get the second item from the array, or a possible third, or possible fourth, etc.

 

I thought adding () around it and giving it a + would do this. But it doesn't seem to do this.

Anyone know what I did wrong here?

Link to comment
Share on other sites

How about something like this?

 

<pre>
<?php
$data = <<<DATA
\$my_array = array(
    'key' => "value",
    'another_key' => 'another_value'
);
DATA;

preg_match_all('/^\s*(\$\S+\s*=\s*array\(.*?\);)/ms', $data, $matches);
array_shift($matches);
print_r($matches);
foreach ($matches[0] as &$match) {
	preg_match('/^\s*\$(\S+)/', $match, $name);
	eval('$array = ' . $match);
	$result[$name[1]] = $array;
}

print_r($result);
?>
</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.