| Line 1... |
Line 1... |
| 1 |
<?php |
1 |
<?php |
| 2 |
// +----------------------------------------------------------------------+ |
2 |
// +----------------------------------------------------------------------+ |
| 3 |
// | PHP Version 4 | |
3 |
// | PHP Version 4 | |
| 4 |
// +----------------------------------------------------------------------+ |
4 |
// +----------------------------------------------------------------------+ |
| 5 |
// | Copyright (c) 1997-2004 The PHP Group | |
5 |
// | Copyright (c) 1997-2004 The PHP Group | |
| 6 |
// +----------------------------------------------------------------------+ |
6 |
// +----------------------------------------------------------------------+ |
| 7 |
// | This source file is subject to version 3.0 of the PHP license, | |
7 |
// | This source file is subject to version 3.0 of the PHP license, | |
| 8 |
// | that is bundled with this package in the file LICENSE, and is | |
8 |
// | that is bundled with this package in the file LICENSE, and is | |
| 9 |
// | available at through the world-wide-web at | |
9 |
// | available at through the world-wide-web at | |
| 10 |
// | http://www.php.net/license/3_0.txt. | |
10 |
// | http://www.php.net/license/3_0.txt. | |
| 11 |
// | If you did not receive a copy of the PHP license and are unable to | |
11 |
// | If you did not receive a copy of the PHP license and are unable to | |
| 12 |
// | obtain it through the world-wide-web, please send a note to | |
12 |
// | obtain it through the world-wide-web, please send a note to | |
| 13 |
// | license@php.net so we can mail you a copy immediately. | |
13 |
// | license@php.net so we can mail you a copy immediately. | |
| 14 |
// +----------------------------------------------------------------------+ |
14 |
// +----------------------------------------------------------------------+ |
| 15 |
// | Authors: Aidan Lister <aidan@php.net> | |
15 |
// | Authors: Aidan Lister <aidan@php.net> | |
| 16 |
// +----------------------------------------------------------------------+ |
16 |
// +----------------------------------------------------------------------+ |
| 17 |
// |
17 |
// |
| 18 |
// $Id: fputcsv.php,v 1.2 2005/11/22 08:28:16 aidan Exp $ |
18 |
// $Id: fputcsv.php,v 1.2 2005/11/22 08:28:16 aidan Exp $ |
| 19 |
|
19 |
|
| 20 |
|
20 |
|
| 21 |
/** |
21 |
/** |
| 22 |
* Replace fprintf() |
22 |
* Replace fprintf() |
| 23 |
* |
23 |
* |
| 24 |
* @category PHP |
24 |
* @category PHP |
| 25 |
* @package PHP_Compat |
25 |
* @package PHP_Compat |
| 26 |
* @link http://php.net/function.fprintf |
26 |
* @link http://php.net/function.fprintf |
| 27 |
* @author Twebb <twebb@boisecenter.com> |
27 |
* @author Twebb <twebb@boisecenter.com> |
| 28 |
* @author Aidan Lister <aidan@php.net> |
28 |
* @author Aidan Lister <aidan@php.net> |
| 29 |
* @version $Revision: 1.2 $ |
29 |
* @version $Revision: 1.2 $ |
| 30 |
* @since PHP 5 |
30 |
* @since PHP 5 |
| 31 |
* @require PHP 4.0.0 (user_error) |
31 |
* @require PHP 4.0.0 (user_error) |
| 32 |
*/ |
32 |
*/ |
| 33 |
if (!function_exists('fputcsv')) { |
33 |
if (!function_exists('fputcsv')) { |
| 34 |
function fputcsv($handle, $fields, $delimiter = ',', $enclosure = '"') |
34 |
function fputcsv($handle, $fields, $delimiter = ',', $enclosure = '"') |
| 35 |
{ |
35 |
{ |
| 36 |
// Sanity Check |
36 |
// Sanity Check |
| 37 |
if (!is_resource($handle)) { |
37 |
if (!is_resource($handle)) { |
| 38 |
user_error('fputcsv() expects parameter 1 to be resource, ' . |
38 |
user_error('fputcsv() expects parameter 1 to be resource, ' . |
| 39 |
gettype($handle) . ' given', E_USER_WARNING); |
39 |
gettype($handle) . ' given', E_USER_WARNING); |
| 40 |
return false; |
40 |
return false; |
| 41 |
} |
41 |
} |
| 42 |
|
42 |
|
| 43 |
|
43 |
|
| 44 |
$str = ''; |
44 |
$str = ''; |
| 45 |
foreach ($fields as $cell) { |
45 |
foreach ($fields as $cell) { |
| 46 |
$cell = str_replace($enclosure, $enclosure . $enclosure, $cell); |
46 |
$cell = str_replace($enclosure, $enclosure . $enclosure, $cell); |
| 47 |
|
47 |
|
| 48 |
if (strchr($cell, $delimiter) !== false || |
48 |
if (strchr($cell, $delimiter) !== false || |
| 49 |
strchr($cell, $enclosure) !== false || |
49 |
strchr($cell, $enclosure) !== false || |
| 50 |
strchr($cell, "\n") !== false) { |
50 |
strchr($cell, "\n") !== false) { |
| 51 |
|
51 |
|
| 52 |
$str .= $enclosure . $cell . $enclosure . $delimiter; |
52 |
$str .= $enclosure . $cell . $enclosure . $delimiter; |
| 53 |
} else { |
53 |
} else { |
| 54 |
$str .= $cell . $delimiter; |
54 |
$str .= $cell . $delimiter; |
| 55 |
} |
55 |
} |
| 56 |
} |
56 |
} |
| 57 |
|
57 |
|
| 58 |
fputs($handle, substr($str, 0, -1) . "\n"); |
58 |
fputs($handle, substr($str, 0, -1) . "\n"); |
| 59 |
|
59 |
|
| 60 |
return strlen($str); |
60 |
return strlen($str); |
| 61 |
} |
61 |
} |
| 62 |
} |
62 |
} |
| 63 |
|
63 |
|
| 64 |
?> |
64 |
?> |
| 65 |
|
65 |
|