0

我正在尝试编写一个制表符分隔的文本文件。其中一个字段是包含 HTML 的产品描述,它会导致问题(我没有进行测试,但效果很好)。在第 95 行,结构中断。我检查了第 95 行的字段,它是纯文本(没有中断或制表符)。

某些东西导致文件混乱。我尝试过修剪、strip_tags、utf8_encode、删除多个空格并通过“/n”将字符串吐入数组中(然后内爆)。

我的代码:

///////////// Get product data from database and store as array /////////////

$products = array();
$c=0;

    $fprod = mysql_query("SELECT id,name,desc FROM products");
    while ($sprod = mysql_fetch_array($fprod)) {

    $products[$c]["id"] = trim($sprod["id"]);
    $products[$c]["name"] = trim($sprod["name"]);

    // strip product description field (the part that doesn't work)

        $prod_desc_a = utf8_encode(strip_tags($sprod["desc"]));    

        // convert any breaks or tabs to \n, explode by \n and then implode

        $prod_desc_b = str_replace(array("\r\n", "\r", "\t", " \t "), "\n", $prod_desc_a);
        $lines = explode("\n", $prod_desc_b);
        $new_lines = array(); 

        foreach ($lines as $i => $line) {
        if(!empty($line))
        $new_lines[] = trim($line);
        }

    $prod_desc_c = implode($new_lines);

// finally remove any multiple spaces and store in array

$prod_desc_final = preg_replace('/\s+/', ' ',$prod_desc_c);


$products[$c]["desc"] = $prod_desc_final;

$c++;
}


///////////// Write the file /////////////

$myFile = "feeds/froogle.txt";
$fh = fopen($myFile, 'w');

    $newline= " \n ";
    $tab= " \t ";

    foreach ($products as $product) {
    $this_line = $product[id] . $tab. $product[name] . $tab . $product[desc] . PHP_EOL; 
    fwrite($fh, $this_line);     
    $this_line = '';
    }

fclose($fh);

任何想法为什么当我导入 Excel 或 Google 文档时文件会中断?非常感谢。

4

0 回答 0