问题
Is there a php library I can use to convert an associative array in perl to an associative array in php ? If not, is there any recommended way of doing this ? Regular expressions ? A bunch of explode and implode calls ?
I want to go from
my %arrayname = (
key1 => "Value1",
key2 => "Value2",
key3 => "Value3",
...
to
$arrayname = array(
"key1" => "Value1",
"key2" => "Value2",
"key3" => "Value3",
回答1:
If you have Perl on your server, you use:
print 'Array(';
while (($key, $val) = each(%arrayname)) {
print "'$key' => '$val',";
}
print ');';
You can also have a look at the PECL Perl package, this library integrates a Perl parser in PHP.
You can use an online Perl interpreter with example code and working (there is just one extra comma at the end).
I created a regex that almost works. You can try it, but it depends on the structure of the Perl array:
preg_match_all(#\%(.+)\s=|\n(.+).*#);
Example data:
my %arrayname = (
key1 => "Value1",
key2 => "Value2",
key3 => "Value3"
)
You can test it with the Regular Expression Test Tool.
来源:https://stackoverflow.com/questions/4154333/convert-perl-associative-array-to-php-array