用PHP将mysql中的UTF-8字段写入CSV格式

用PHP将mysql中的UTF-8字段写入CSV格式,php,csv,export-to-csv,Php,Csv,Export To Csv,我使用的是一个php类,它将CSV文件写入并存储在我网站的文件夹中。但我无法写入数据库中存在的不同语言的UTF-8字符 我使用下面的类来编写csv文件。如何添加对该类写入utf-8字符的支持。我尝试过谷歌和其他地方,但没有找到任何运气 class b3rtCSVWriter { var $filename; var $delimiter; var $fileHandle; var $fileBuffer; var $fileBufferSize; var $errorList; /*fun

我使用的是一个
php类
,它将
CSV文件写入并存储在我网站的文件夹中。但我无法写入数据库中存在的不同语言的
UTF-8
字符

我使用下面的类来编写csv文件。如何添加对该类写入utf-8字符的支持。我尝试过谷歌和其他地方,但没有找到任何运气

class b3rtCSVWriter
{
var $filename;
var $delimiter;

var $fileHandle;
var $fileBuffer;
var $fileBufferSize;

var $errorList;

/*function b3rtCSVWriter()
    {
    }*/

    function __construct()
    {
    $this->filename = '';
    $this->delimiter = ',';

    $this->fileHandle = NULL;
    $this->fileBuffer = '';
    $this->fileBufferSize = 4096;

    $this->errorList = array();
    }

    function __destruct()
    {
    $this->closeFile();
    }

    function setFilename($filename)
    {
    $this->filename = $filename;

    return $this->openFile();
    }

    function setDelimiter($delimiter)
    {
    if (strlen($delimiter) != 1)
        {
        $this->setError('Invalid delimiter');
        return FALSE;
        }

    $this->delimiter = $delimiter;
    return TRUE;
    }

    function getErrors()
    {
    return $this->errorList;
    }

    function putRecord($recordData)
    {
    if ($this->errorList)
        return ($this->fileHandle === NULL ? '' : FALSE);

    $rowBuffer = '';

    // Check if recordData is an array
    if (isset($recordData) && is_array($recordData))
        {
        $currentFieldIndex = -1;
        $lastFieldIndex = count($recordData) - 1;

        // Loop through every array item
        foreach($recordData as $recordValue)
            {
            $currentFieldIndex++;
            $isQuotedField = FALSE;

            // Set isQuotedField if a " is present and replace " with ""
            if (strpos($recordValue, '"') !== FALSE)
                {
                $isQuotedField = TRUE;
                $recordValue = str_replace('"', '""', $recordValue);
                }

            // Set isQuotedField if a delimiter or newline is present
            if ((strpos($recordValue, $this->delimiter) !== FALSE) ||
                (strpos($recordValue, "\n") !== FALSE))
                $isQuotedField = TRUE;

            // Put field inside " if isQuotedField is set
            if ($isQuotedField)
                $recordValue = '"'.$recordValue.'"';

            // Add recordValue to rowBuffer and, if not at last field, a delimiter
            $rowBuffer .= $recordValue .
                ($currentFieldIndex != $lastFieldIndex ? $this->delimiter : '');
            }
        }

    // Add EOL to rowBuffer, even when it's empty
    $rowBuffer .= "\r\n";

    // If no file is currently open, return rowBuffer as is
    if ($this->fileHandle === NULL)
        return $rowBuffer;
    else // Else write rowBuffer to file
        return $this->writeFile($rowBuffer);
    }

    function openFile()
    {
    $this->closeFile();

    if ($this->filename == '')
        $this->setError('Invalid filename');

    if ($this->errorList)
        return FALSE;

    $this->fileHandle = @fopen($this->filename, 'w');
    if (!$this->fileHandle)
        {
        $this->setError('Could not open file');
        $this->fileHandle = NULL;
        return FALSE;
        }

    if (!flock($this->fileHandle, LOCK_EX))
        {
        $this->setError('Could not lock file');
        $this->closeFile();
        return FALSE;
        }

    return TRUE;
    }

    function writeFile($toWrite, $forceWrite = FALSE)
    {
    if ($this->fileHandle === NULL)
        {
        $this->setError('No file specified');
        return FALSE;
        }

    $this->fileBuffer .= $toWrite;

    if ($forceWrite || (strlen($this->fileBuffer) > $this->fileBufferSize))
        {
        if (@fwrite($this->fileHandle, $this->fileBuffer, strlen($this->fileBuffer)) === FALSE)
            {
            $this->setError('Could not write to file');
            return FALSE;
            }

        $this->fileBuffer = '';
        }

    return TRUE;
    }

    function closeFile()
    {
    if (is_resource($this->fileHandle))
        {
        // Force buffer to be written
        $this->writeFile('', TRUE);

        if (!fflush($this->fileHandle))
            $this->setError('Could not flush output to file');
        if (!flock($this->fileHandle, LOCK_UN))
            $this->setError('Could not unlock file');
        if(!@fclose($this->fileHandle))
            $this->setError('Could not close file');
        }

    $this->fileHandle = NULL;
    }

    function setError($error)
    {
    $this->errorList[] = $error;
    }
    }
在浏览了这么多天之后,我发现我需要添加BOM来修复Excel中的UTF-8,但是添加到这个类中失败了

    $fp = fopen('php://output', 'w');
    fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
    fclose($fp);
添加到此处:

$this->fileHandle = @fopen($this->filename, 'w');
if (!$this->fileHandle)
    {
    $this->setError('Could not open file');
    $this->fileHandle = NULL;
    return FALSE;
    }
fputs($this->fileHandle, chr(0xEF) . chr(0xBB) . chr(0xBF));

谢谢你的即时回复,还是给我????对于非英语字符,您确定要用一个全是UTF-8的数组调用
putRecord()
?是的,我像这样使用putRecord()<代码>$csvWriter->putRecord(数组('Phone','Name','Product','delived on','No of Products','marks')是,但一行包含非英语字符。这是UTF-8吗?是的,我也可以在mysql字段中看到它。完全得救了。我也试着用这个<代码>fputs($this->fileHandle,$bom=(chr(0xEF).chr(0xBB).chr(0xBF))