2013-06-28 31 views
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字符串現在我想該字符串基於分割符|添加字符串分隔符從URL來在PHP

轉換爲數組,但每個新結束後出現了問題行沒有放置分隔符|,所以我希望在每行結束後放置該分隔符。

注::所有列將被預定義,並且它將返回相同的所有時間請求。

我使用此代碼將該字符串轉換爲數組。如果將所有分隔符正確放置在字符串中,此代碼工作正常。

$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 

     ) 
     ----- 
     ---- 
) 

回答

1

我會做這個作爲2個步驟。首先將其分割成\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()爲數組中的每個元素調用回調函數(我將其定義爲anonymous function)。在這個例子中,我定義了一個簡單的函數來修剪線條,以刪除可能存在的任何多餘空格,然後將其分割爲|字符以獲取單個字段。

如果array_map線是一個有點複雜,我可以說明它是如何工作的通過重寫它沒有anonymous function這樣的:

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); 
} 
+0

...良好的工作,但沒有更多的\ n在字符串它的簡單strig不再\ n在那... –

+0

@intekhabkhan在'\ \ n'被解釋爲一個換行符 - 它將匹配(並分割)每行的結尾。 – jcsanyi

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 
?> 
+0

我已更新帖子...請檢查..我只得到字符串來自url與每列添加一些分隔符。 –

+0

更新了答案。 – Fracsi

+0

感謝您給我答案但我的要求是,我在看這篇文章的結尾。 –

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); 
} 

這應該產生期望的ouptut的手段相結合,假設你要定界|保持在弦。如果不是:

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

就是這樣,真的...

+0

謝謝..但我想轉換該數組中的字符串..看看這個輸出結尾附加帖子。 –

+0

@intekhabkhan:只需使用'explode(PHP_EOL,$ string)爆炸字符串'並將'preg_split'應用到每一行...創建一個數組,爲每一行將上述代碼的返回值推送到該數組中,你在那裏... –

+0

謝謝@Elias,但有字符串與「|」的組合或空格,也是每行的結尾。如果你能幫助我,我會明白這一點。 根據你的代碼,我得到了這個輸出..我想輸出,就像我在這篇文章 Array([0] => [1] => [2] => Website | Period In Period |總銷售額| 9766期間的總佣金| Mountains Plus戶外裝備| www.MPGear.com | 1 | 0 | 88.91 | 8.89 –