0

我有一个 csv 文件,我需要从中删除部分,它有 7017 行长,所以我使用 php 删除某些部分并将其重新组合在一起,相关代码如下。

$rows;
$count = 0;
while (($row = fgetcsv($files, 56000)) !== false) {
  $rows[$count] = $row;
  if (stristr($rows[$count][8], "Features:")) {
    $rows[$count][8] = str_replace("Features:", "features", $rows[$count][8]);
    $rows[$count][8] = str_replace(":", "", $rows[$count][8]);
    $rows[$count][8] = explode("features", $rows[$count][8]);
    if (isset($rows[$count][8][1])) {
      $rows[$count][8][1] = str_replace("Compatible", "compatible", $rows[$count][8][1]);
      $rows[$count][8][1] = explode("compatible", $rows[$count][8][1]);
      //$rows[$count][8][1] = explode("Compatible", $rows[$count][8][1]); produces error if  used
      $rows[$count][8][1] = explode("-", $rows[$count][8][1][0]);
    } else if (isset($rows[$count][8][0])) {
      $rows[$count][8][0] = str_replace("Compatible", "compatible", $rows[$count][8][0]);
      $rows[$count][8][0] = explode("compatible", $rows[$count][8][0]);
      //$rows[$count][8][0] = explode("Compatible", $rows[$count][8][0]); produces error if  used
      $rows[$count][8][0] = explode("-", $rows[$count][8][0][0]);
    } else {
      $rows[$count][8] = str_replace("Compatible", "compatible", $rows[$count][8]);
      $rows[$count][8] = explode("compatible", $rows[$count][8]);
      //$rows[$count][8] = explode("Compatible", $rows[$count][8]); produces error if  used
      $rows[$count][8] = explode("-", $rows[$count][8][0]);
    }
    $countif = 1;
    while (isset($rows[$count][8][1][0][$countif])) {
      $countif++;
    }
    $max = $countif - 2;
    $countif = 9;
    $counter = 1;
    while ($max <= 5 && $max >= 1 && $countif <= 13) {
      $rows[$count][$countif] = $rows[$count][8][1][0][$counter];
      $countif++;
      $counter++;
    }
    $rows[$count][8][0] = explode("Compatible", $rows[$count][8][0]);
    //$rows[$count][8] = $rows[$count][8][0][0];

  }
  $count++;
}

print_r($rows[14][8]);

问题是即使 'Compatible' 在explode 语句中引用的字符串中,它仍然是一个字符串,可以说是未分解的。输出如下。

100% 全新 采用优质材料制成,确保质量好 防止划痕 屏幕上出现油脂和指纹 您现在可以使用手写笔轻松使用手机或文本人 设计用于您的触摸屏手机兼容:诺基亚: 5530 Xpress 音乐包装内容:1 x 用于诺基亚 5530 Xpress 音乐的触控笔 - 白色

任何帮助是极大的赞赏。

编辑var_dump($rows[14]);

0 => string '41807' (length=5)
  1 => string '' (length=0)
  2 => string 'http://images.esellerpro.com/2477/I/418/07/07_!B-dzTQ!BWk~$(KGrHqQOKnMEy1t9tWyLBM8tHiFEnQ~~0_1.JPG' (length=98)
  3 => string '' (length=0)
  4 => string '' (length=0)
  5 => string '' (length=0)
  6 => string '5055496500300    ' (length=17)
  7 => string 'STYLUS-5530-WHITE - STOCK CONTROL' (length=33)
  8 => string ' 100% Brand New Made from high quality material to ensure good quality Prevent Scratches bumps grease and finger prints on the screen You can now easily use the mobile or text people with the stylus Designed to be used with&nbsp;your touch screen mobile&nbsp;Compatible with:&nbsp;Nokia : 5530 Xpress MusicPackage Contents: 1&nbsp;x Stylus for Nokia 5530 Xpress Music -&nbsp;White&nbsp;' (length=386)
  9 => string '' (length=0)
  10 => string '' (length=0)
  11 => string '' (length=0)
  12 => string '' (length=0)
  13 => string '' (length=0)
  14 => string '-1' (length=2)
  15 => string '' (length=0)
  16 => string '' (length=0)
  17 => string 'Frooition' (length=9)
  18 => string '' (length=0)

EDIT2:原始行:

3PREPAY-ZTEMF627W1GB        http://images.esellerpro.com/2477/I/217/94/03_!B8Zel8w!2k~$(KGrHqJ!hYEyrrVkKNYBM28dwn4o!~~0_1.JPG               5.0555E+12  ZTE MF627 THREE PAY BROADBAND USB MODEM WITH 1GB READY  New ZTE MF627 on Three Pay Broadband with 1GB allowance IncludedGet broadband speeds with no wires or hassles. Just plug this lightweight USB Modem into your laptop and watch as everything auto-installs whether you’re on Windows or Mac. Easy. The user friendly dashboard is also a breeze (so you don’t need any technical knowledge) and it lets you send and receive text messages while you’re online.The MF627 gives you download speeds of up to 3.6 Mbps. Plus you can use your new best friend as a memory stick just add a MicroSD card to the slot and load up to 4GB. And don’t ever worry about losing the lid because it’s connected with a handy strap! Compatible with: Most Desktops and Laptops (Also supports Windows 7 and Mac OS X 10.6)Packing Contents:1 X Brand New ZTE MF627 USB Model on Three Pay as you go Broadband  - 1GB Allowance Included Maximum 1 per customer please.                       -1          Frooition
4

2 回答 2

1

这是你的问题..

这是你的第二次爆炸...

$rows[$count][8][0] = explode("Compatible", $rows[$count][8][0]); 

如果该行不包含 Features 则 $count[8] 不会分解并转换为数组。在你的 var_dump $rows[$count][8] 只是一个字符串, $rows[$count][8][0] 不存在。换行来做到这一点..

$rows[$count][8] = explode("Compatible", $rows[$count][8]);

但是您可能需要考虑将其放在 else 语句中而不是更改它。因为如果字符串包含 Features 并且已爆炸,您现在将拥有的内容将起作用。所以我建议的行应该放在 Else 语句中,以防 Features 不是字符串的一部分,并且 $rows[$count][8] 永远不会爆炸。

$rows;
$count = 0;
while (($row = fgetcsv($files, 56000)) !== false) {
  $rows[$count] = $row;
  if (stristr($rows[$count][8], "Features:")) {
    $rows[$count][8] = str_replace("Features:", "features", $rows[$count][8]);
    $rows[$count][8] = str_replace(":", "", $rows[$count][8]);
    $rows[$count][8] = explode("features", $rows[$count][8]);
    if (isset($rows[$count][8][1])) {
      $rows[$count][8][1] = str_replace("Compatible", "compatible", $rows[$count][8][1]);
      $rows[$count][8][1] = explode("compatible", $rows[$count][8][1]);
      //$rows[$count][8][1] = explode("Compatible", $rows[$count][8][1]); produces error if  used
      $rows[$count][8][1][0] = explode("-", $rows[$count][8][1][0]);
    } else if (isset($rows[$count][8][0])) {
      $rows[$count][8][0] = str_replace("Compatible", "compatible", $rows[$count][8][0]);
      $rows[$count][8][0] = explode("compatible", $rows[$count][8][0]);
      //$rows[$count][8][0] = explode("Compatible", $rows[$count][8][0]); produces error if  used
      $rows[$count][8][0][0] = explode("-", $rows[$count][8][0][0]);
    } else {
      $rows[$count][8] = str_replace("Compatible", "compatible", $rows[$count][8]);
      $rows[$count][8] = explode("compatible", $rows[$count][8]);
      //$rows[$count][8] = explode("Compatible", $rows[$count][8]); produces error if  used
      $rows[$count][8] = explode("-", $rows[$count][8][0]);
    }
    $countif = 1;
    while (isset($rows[$count][8][1][0][$countif])) {
      $countif++;
    }
    $max = $countif - 2;
    $countif = 9;
    $counter = 1;
    while ($max <= 5 && $max >= 1 && $countif <= 13) {
      $rows[$count][$countif] = $rows[$count][8][1][0][$counter];
      $countif++;
      $counter++;
    }
    $rows[$count][8][0] = explode("Compatible", $rows[$count][8][0]);
    //$rows[$count][8] = $rows[$count][8][0][0];

  }
  else // THE STRING DOESN'T CONTAIN "Features"
  {
        $rows[$count][8] = explode("Compatible", $rows[$count][8]);
  }
  $count++;
}
于 2012-04-19T14:48:01.127 回答
0

如果您的数据看起来像我想的那样,则不要检查字符串中是否存在“兼容”。关于什么:

if (stristr($rows[$count][8], "Features:")) {
  $rows[$count][8] = str_replace("Features:", "features", $rows[$count][8]);
  $rows[$count][8] = str_replace(":", "", $rows[$count][8]);
  $rows[$count][8] = explode("features", $rows[$count][8]);
} elseif (stristr($rows[$count][8], "Compatible:")) {
  $rows[$count][8][1] = str_replace("Compatible", "compatible", $rows[$count][8][1]);
  $rows[$count][8][1] = explode("compatible", $rows[$count][8][1]);
  $rows[$count][8][1][0] = explode("-", $rows[$count][8][1][0]);
...
于 2012-04-19T14:32:40.300 回答