0
 Merchant Id|Merchant|Website|Transaction In Period|Voids In Period|Gross Sales Amount|Total Commision in Period         
 9766|Mountains Plus Outdoor Gear|www.MPGear.com|1|0|88.91|8.89 
 12447|Meritline.com|www.meritline.com|5|0|52.86|3.71
 16213|East Coast Fashions|www.discountstripper.com|1|0|32.27|3.23
 17226|HRM USA|www.HeartRateMonitorsUSA.com|1|0|189.9|6.65

我现在从 url 获取上面的字符串,现在我想根据拆分分隔符将该字符串转换为数组|

但是在每个新行结束后都有一个问题,没有放置分隔符|,所以我希望在每行结束后放置分隔符。

注意::所有列都将被预定义,并且它将始终返回相同的请求。

我正在使用此代码将该字符串转换为数组。如果将所有分隔符正确放置在字符串中,则此代码将完美运行。

$NumColumns = 6;
$Delim = "|";
$data = array_chunk(explode($Delim,$contents), $NumColumns); 

输出将是这样的

Array
(
    [0] => Array
        (
            [0] => Merchant Id
            [1] => Merchant
            [2] => Website
            [3] => Transaction In Period
            [4] => Voids In Period
            [5] => Gross Sales Amount
            [6] => Total Commision in Period 
        )

    [1] => Array
        (
            [0] => 9766
            [1] => Mountains Plus Outdoor Gear
            [2] => www.MPGear.com
            [3] => 1
            [4] => 0
            [5] => 88.91
            [6] => 8.89

        )  
        -----
        ----
)
4

3 回答 3

3

尝试使用str_getcsvexplode

<?php
    $data_from_url = "Merchant Id|Merchant|Website|Transaction In Period|Voids In Period|Gross Sales Amount|Total Commision in Period
9766|Mountains Plus Outdoor Gear|www.MPGear.com|1|0|88.91|8.89 
12447|Meritline.com|www.meritline.com|5|0|52.86|3.71
16213|East Coast Fashions|www.discountstripper.com|1|0|32.27|3.23
17226|HRM USA|www.HeartRateMonitorsUSA.com|1|0|189.9|6.65";
    $splitted_data = explode(PHP_EOL, $data_from_url);
    /**
     * print_r($splitted_data) will be
     * Array
     * (
         [0] => "Merchant Id|Merchant|Website|Transaction In Period|Voids In Period|Gross Sales Amount|Total Commision in Period"
         [1] => "9766|Mountains Plus Outdoor Gear|www.MPGear.com|1|0|88.91|8.89"
         ...
     * )
     */
     // You can now iterate through the lines
     $output1 = array();
     foreach($splitted_data as $row) {
        $output1[] = str_getcsv(trim($row), '|'); // parses a 
        // OR
        //$output1[] = explode('|', trim($row));
     }
     // OR use array_map with callback function
     $output2 = array_map(function($line){ return explode('|', trim($line)); }, $splitted_data);
     $output3 = array_map(function($line){ return str_getcsv(trim($line), '|'); }, $splitted_data);
     var_dump($output1, $output2, $output3); // The result you want to achive
?>
于 2013-06-28T06:52:39.957 回答
1

我会将此作为一个两步过程。首先将其拆分为\n(换行符)字符上的行,然后拆分|字符上的每一行。

您只需几行就可以做到这一点,如下所示:

$lines = explode("\n", $contents);
$data = array_map(function($line) {return explode('|', trim($line));}, $lines);

你可以在这里看到这个工作:http: //phpfiddle.org/main/code/9fv-h2c

将内容拆分为单独的行后,我将使用该array_map()函数将相同的操作应用于数组的每个元素(每一行)。

array_map()为数组中的每个元素调用一个回调函数(我将其定义为匿名函数)。在本例中,我定义了一个简单的函数,它修剪行以删除可能存在的任何额外空格,然后将其拆分到|字符上以获取各个字段。

如果 array_map 行有点复杂,我可以通过在没有匿名函数的情况下重写它来说明它是如何工作的:

function processLine($line) {
    $line = trim($line);
    $fields = explode('|', $line);
    return $fields;
}
$data = array_map('processLine', $lines);

...甚至在不使用 array_map 的情况下重写它,如下所示:

function processLine($line) {
    $line = trim($line);
    $fields = explode('|', $line);
    return $fields;
}
$data = array();
foreach ($lines as $l) {
    $data[] = processLine($l);
}
于 2013-06-28T07:30:44.417 回答
1

如果我正确理解您的问题,您正在尝试拆分数据,但希望将分隔符保留|为字符串的一部分。
使用preg_split,您可以这样做:

$arr = preg_split('/([^|]*\|)/', $string, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

该表达式匹配零个或多个不是 |( [^|]*)的字符
并匹配定界符|. 两者的组合用作分隔符。换句话说,现在一切都是分隔符。
这就是我们必须使用预定义常量的原因PREG_SPLIT_DELIM_CAPUTRE

当然,在分隔符之间,什么都没有,但preg_split也会捕获这种虚无,并在结果数组中添加空匹配项。这就是为什么我们必须使用另一个预定义常量:PREG_SPLIT_NO_EMPTY.
这两个常数通过按位或运算符组合在一起|

$output = explode(PHP_EOL, $input);
foreach($output as &$line)
{//$line is a reference here, important!
    $line = preg_split('/([^|]*\|)/', $line, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
}

|假设您希望将分隔符保留在字符串中,这应该会产生所需的输出。如果不:

$output = explode(PHP_EOL, $input);
foreach($output as &$line)
{
    $line = explode('|', $line);
}

就是这样,真的...

于 2013-06-28T07:01:03.817 回答