I am trying to parse a file using php but I am not sure of the best way to do it. The file consists of stuff like:
我试图使用PHP解析文件,但我不知道最好的方法来做到这一点。该文件包含以下内容:
saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1
What I'm trying to do is split it up and populate a hash map, so..
我想要做的是将它拆分并填充哈希映射,所以..
$inventory["saturn+5"] = "57";
$inventory["space+shuttle"] = "34";
and so on.
I don't know how to tackle this.
我不知道如何解决这个问题。
I am trying to write a bit of regex to process the file to separate out the fields but I'm not having much luck and was wondering if I should try using a different approach using split()
or explode()
.
我正在尝试编写一些正则表达式来处理文件以分离出字段,但我没有太多运气,并且想知道我是否应该尝试使用split()或explode()来使用不同的方法。
4 个解决方案
#1
2
Here's my approach using regular expression.
这是我使用正则表达式的方法。
$data = 'saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1';
$inventory = array();
preg_match_all('/(\S+) (\S+)/', $data, $matches);
foreach ($matches[1] as $index => $match) {
$inventory[$match] = $matches[2][$index];
}
print_r($inventory);
Output
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)
#2
2
my crude approach:
我原油的做法:
<?php
echo '<pre>';
$str="saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1";
//break it on space
$e=explode(' ',$str);
//reindex array to start from 1
array_unshift($e, "phoney");
unset($e[0]);
print_r($e);
$inventory=array();
foreach ($e as $k=>$v){
//detects odd key
if(($k+2)%2==1) {
$inventory[$v]= $e[$k+1];
}
}
print_r($inventory);
demo: http://codepad.viper-7.com/PN6K8m
output:
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)
#3
2
It's actually quite trivial with a regex:
对于正则表达式来说,这实际上是微不足道的:
preg_match_all("/ ([\w+]+) \s (\d+) /x", $string, $m);
$assoc = array_combine($m[1], $m[2]);
You're just looking for a combination of alphanumeric characters \w
and optional +
signs, then a space, then a \d
decimal.
你只是在寻找一个字母数字字符\ w和可选+符号的组合,然后是空格,然后是\ d十进制。
array_combine
will give you the associative array.
array_combine将为您提供关联数组。
#4
2
If it's always in that order, this will work:
如果它始终按此顺序,这将工作:
<?
$foo = 'saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1';
$foo_array = preg_split('/\s+/', $foo);
$hash = array();
for ($i = 0; $i < count($foo_array); $i++){
$i % 2 ? null : $hash[$foo_array[$i]] = $foo_array[++$i];
}
print_r($hash);
?>
Output:
php foo.php
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)
#1
2
Here's my approach using regular expression.
这是我使用正则表达式的方法。
$data = 'saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1';
$inventory = array();
preg_match_all('/(\S+) (\S+)/', $data, $matches);
foreach ($matches[1] as $index => $match) {
$inventory[$match] = $matches[2][$index];
}
print_r($inventory);
Output
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)
#2
2
my crude approach:
我原油的做法:
<?php
echo '<pre>';
$str="saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1";
//break it on space
$e=explode(' ',$str);
//reindex array to start from 1
array_unshift($e, "phoney");
unset($e[0]);
print_r($e);
$inventory=array();
foreach ($e as $k=>$v){
//detects odd key
if(($k+2)%2==1) {
$inventory[$v]= $e[$k+1];
}
}
print_r($inventory);
demo: http://codepad.viper-7.com/PN6K8m
output:
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)
#3
2
It's actually quite trivial with a regex:
对于正则表达式来说,这实际上是微不足道的:
preg_match_all("/ ([\w+]+) \s (\d+) /x", $string, $m);
$assoc = array_combine($m[1], $m[2]);
You're just looking for a combination of alphanumeric characters \w
and optional +
signs, then a space, then a \d
decimal.
你只是在寻找一个字母数字字符\ w和可选+符号的组合,然后是空格,然后是\ d十进制。
array_combine
will give you the associative array.
array_combine将为您提供关联数组。
#4
2
If it's always in that order, this will work:
如果它始终按此顺序,这将工作:
<?
$foo = 'saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1';
$foo_array = preg_split('/\s+/', $foo);
$hash = array();
for ($i = 0; $i < count($foo_array); $i++){
$i % 2 ? null : $hash[$foo_array[$i]] = $foo_array[++$i];
}
print_r($hash);
?>
Output:
php foo.php
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)