jay3ld Posted October 24, 2008 Share Posted October 24, 2008 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 https://forums.phpfreaks.com/topic/129910-collecting-information-from-an-array/ Share on other sites More sharing options...
sasa Posted October 24, 2008 Share Posted October 24, 2008 2nd item from $my_array $keys = array_keys($my_array); echo $my_array[$keys[1]]; Link to comment https://forums.phpfreaks.com/topic/129910-collecting-information-from-an-array/#findComment-673539 Share on other sites More sharing options...
jay3ld Posted October 24, 2008 Author Share Posted October 24, 2008 The problem is the php code with the array. Is basically plain text and not parsable without causing issues.. I am wanting to pull out its information though. Link to comment https://forums.phpfreaks.com/topic/129910-collecting-information-from-an-array/#findComment-673723 Share on other sites More sharing options...
sasa Posted October 24, 2008 Share Posted October 24, 2008 are you try this <?php $text = '$my_array = array( \'key\' => "value", \'another_key\' => \'another_value\' );'; eval($text); print_r($my_array); ?> Link to comment https://forums.phpfreaks.com/topic/129910-collecting-information-from-an-array/#findComment-673771 Share on other sites More sharing options...
effigy Posted October 24, 2008 Share Posted October 24, 2008 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 https://forums.phpfreaks.com/topic/129910-collecting-information-from-an-array/#findComment-673781 Share on other sites More sharing options...
jay3ld Posted October 24, 2008 Author Share Posted October 24, 2008 Thank you, While it wasn't exactly what I wanted. After some tweaking I got it working exactly how I needed it to. Link to comment https://forums.phpfreaks.com/topic/129910-collecting-information-from-an-array/#findComment-673911 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.