Skip to content

Commit ce6aec7

Browse files
committed
Merge pull request #20 from hdogan/utf8-bom-support
Added _bom variable to support UTF-8 BOM output
2 parents 7b80daf + 18ea01d commit ce6aec7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public function export() {
110110
}
111111
```
112112

113-
You can also specify the delimiter, end of line, newline and escape characters using
114-
`$_delimiter`, `$_eol`, `$_newline` and `$_enclosure`, respectively:
113+
You can also specify the delimiter, end of line, newline, escape characters and byte order mark (BOM) sequence using
114+
`$_delimiter`, `$_eol`, `$_newline`, `$_enclosure` and `$_bom` respectively:
115115

116116
```php
117117
public function export() {
@@ -126,6 +126,7 @@ public function export() {
126126
$_enclosure = '"';
127127
$_newline = '\r\n';
128128
$_eol = '~';
129+
$_bom = true;
129130

130131
$this->viewClass = 'CsvView.Csv';
131132
$this->set(compact('data', '_serialize', '_delimiter', '_enclosure', '_newline', '_eol'));
@@ -138,11 +139,14 @@ The defaults for these variables are:
138139
* `_enclosure`: `"`
139140
* `_newline`: `\n`
140141
* `_eol`: `\n`
142+
* `_bom`: false
141143

142144
The `_eol` variable is the one used to generate newlines in the output.
143145
`_newline`, however, is the character that should replace the newline characters in the actual data.
144146
It is recommended to use the string representation of the newline character to avoid rendering invalid output.
145147

148+
Some reader software incorrectly renders UTF-8 encoded files which do not contain byte order mark (BOM) byte sequence. The `_bom` variable is the one used to add byte order mark (BOM) byte sequence beginning of the generated CSV output stream. See [`Wikipedia article about byte order mark`](http://en.wikipedia.org/wiki/Byte_order_mark) for more information.
149+
146150
If you have complex model data, you can use the `$_extract` view variable to specify the individual paths for each record. This is an array of `Hash::extract()`-compatible syntax:
147151

148152
```php

View/CsvView.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public function render($view = null, $layout = null) {
145145
* - '_enclosure': (default '"') CSV Enclosure for use with fputscsv()
146146
* - '_newline': (default '\n') CSV Newline replacement for use with fputscsv()
147147
* - '_eol': (default '\n') End-of-line character the csv
148+
* - '_bom': (default false) Adds BOM (byte order mark) header
148149
*
149150
* @return void
150151
**/
@@ -158,7 +159,8 @@ protected function _setupViewVars() {
158159
'_enclosure',
159160
'_newline',
160161
'_eol',
161-
'_null'
162+
'_null',
163+
'_bom'
162164
);
163165
foreach ($required as $viewVar) {
164166
if (!isset($this->viewVars[$viewVar])) {
@@ -186,6 +188,10 @@ protected function _setupViewVars() {
186188
$this->viewVars['_null'] = 'NULL';
187189
}
188190

191+
if ($this->viewVars['_bom'] === null) {
192+
$this->viewVars['_bom'] = false;
193+
}
194+
189195
if ($this->viewVars['_extract'] !== null) {
190196
$this->viewVars['_extract'] = (array)$this->viewVars['_extract'];
191197
foreach ($this->viewVars['_extract'] as $i => $extract) {
@@ -261,6 +267,10 @@ protected function _generateRow($row = null) {
261267
static $fp = false;
262268
if ($fp === false) {
263269
$fp = fopen('php://temp', 'r+');
270+
271+
if ($this->viewVars['_bom']) {
272+
fputs($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
273+
}
264274
} else {
265275
rewind($fp);
266276
}

0 commit comments

Comments
 (0)