2011-01-21 38 views
0

在Linux上创建文本文件格式为Dos \ Windows并以UTF-8编码时出现问题。使用Dos Windows编码文本文件并使用UTF-8编码为PHP

什么Im做是这样的:

while($row = mysql_fetch_array($result)) 
{ 
    $product = $row['products_id'] 
    . ' ' . $row['products_name'] 
    . ' ' . $row['manufacturers_name'] 
    . ' ' . $row['products_model'] 
    //. ' ' . $row['products_sku'] 
    . ' ' . zen_href_link('product_info', 'cPath=' . $cPath . '&products_id=' . $row['products_id']) 
    . ' ' . number_format($row['products_price'],2) 
    . ' ' . strip_tags(zen_get_products_description($row['products_id'])) 
    . ' ' . HTTP_SERVER . '/' .DIR_WS_IMAGES . $row['products_image'] 
    . ' ' . 'electronics' 
    . "\r\n"; 
$product = utf8_encode($product); 
fwrite($fh, gzencode($product,9)); 

我的印象是“\ r \ n”,将编码作为其完成所有需要的多数民众赞成下。我在这里错过了什么吗?

回答

0

我怀疑你想成为gz编码记录那样的记录。

如果您需要将其压缩,请考虑整理它,然后在最后写入一次,或者gzipping创建文件。

$productList = ''; 

while($row = mysql_fetch_array($result)) { 
    $product = $row['products_id'] 
    . ' ' . $row['products_name'] 
    . ' ' . $row['manufacturers_name'] 
    . ' ' . $row['products_model'] 
    //. ' ' . $row['products_sku'] 
    . ' ' . zen_href_link('product_info', 'cPath=' . $cPath . '&products_id=' . $row['products_id']) 
    . ' ' . number_format($row['products_price'],2) 
    . ' ' . strip_tags(zen_get_products_description($row['products_id'])) 
    . ' ' . HTTP_SERVER . '/' .DIR_WS_IMAGES . $row['products_image'] 
    . ' ' . 'electronics' 
    . "\r\n"; 

    $productList .= $product; 
} 

fwrite($fh, gzencode(utf8_encode($product),9)); 
fclose($fh); 
0

"\r\n"是正确的。如果数据库表已经包含UTF-8,则utf8_encode()是冗余的。但是我看到的真正问题是您应该只包装while {...}循环中的$ product变量的串联。您无法为每行重新启动gzencode并将块写入文件。当您尝试打开它时会导致错误。与其多次调用gzencode,只需打开输出文件gzopen()而不是fopen()