Eri*_*rik 11 php csv filehandle
我在服务器上,我只限于PHP 5.2.6,这意味着我无法使用str_getcsv.我正在使用,而不是fgetcsv,它需要"一个有效的文件指针,指向由fopen(),popen()或fsockopen()成功打开的文件." 操作.
我的问题是:有没有办法将字符串作为文件句柄访问?
我的另一个选择是将字符串写入文本文件然后通过fopen()然后使用来访问它fgetcsv,但我希望有一种方法可以直接执行此操作,例如在perl中.
Pas*_*TIN 21
如果你查看手册页上的用户注释str_getcsv,你会发现daniel的这个注释,它提出了这个功能(引用):
<?php
if (!function_exists('str_getcsv')) {
function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") {
$fiveMBs = 5 * 1024 * 1024;
$fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
fputs($fp, $input);
rewind($fp);
$data = fgetcsv($fp, 1000, $delimiter, $enclosure); // $escape only got added in 5.3.0
fclose($fp);
return $data;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
它似乎完全符合您的要求:它使用一个流,它指向内存中的临时文件句柄,以便fgetcsv在其上使用.
有关(尤其是流包装器)的文档,
请参阅PHP输入/输出流php://temp.
当然,你应该测试它对你有用 - 但是,至少,这应该让你知道如何实现这个;-)
要回答您的一般问题,是的,您可以将变量视为文件流.
http://www.php.net/manual/en/function.stream-context-create.php
以下是PHP手册中几个不同注释的复制和粘贴(因此我无法保证生产准备就绪):
<?php
class VariableStream {
private $position;
private $varname;
public function stream_open($path, $mode, $options, &$opened_path) {
$url = parse_url($path);
$this->varname = $url["host"];
$this->position = 0;
return true;
}
public function stream_read($count) {
$p=&$this->position;
$ret = substr($GLOBALS[$this->varname], $p, $count);
$p += strlen($ret);
return $ret;
}
public function stream_write($data){
$v=&$GLOBALS[$this->varname];
$l=strlen($data);
$p=&$this->position;
$v = substr($v, 0, $p) . $data . substr($v, $p += $l);
return $l;
}
public function stream_tell() {
return $this->position;
}
public function stream_eof() {
return $this->position >= strlen($GLOBALS[$this->varname]);
}
public function stream_seek($offset, $whence) {
$l=strlen(&$GLOBALS[$this->varname]);
$p=&$this->position;
switch ($whence) {
case SEEK_SET: $newPos = $offset; break;
case SEEK_CUR: $newPos = $p + $offset; break;
case SEEK_END: $newPos = $l + $offset; break;
default: return false;
}
$ret = ($newPos >=0 && $newPos <=$l);
if ($ret) $p=$newPos;
return $ret;
}
}
stream_wrapper_register("var", "VariableStream");
$csv = "foo,bar\ntest,1,2,3\n";
$row = 1;
if (($handle = fopen("var://csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
Run Code Online (Sandbox Code Playgroud)
当然,对于您的特定示例,可以使用更简单的流方法.
我很震惊没有人回答这个问题:
<?php
$string = "I tried, honestly!";
$fp = fopen('data://text/plain,' . $string,'r');
echo stream_get_contents($fp);
#fputcsv($fp, .......);
?>
Run Code Online (Sandbox Code Playgroud)
而内存饥渴的完美解决方案:
<?php
class StringStream
{
private $Variable = NULL;
protected $fp = 0;
final public function __construct(&$String, $Mode = 'r')
{
$this->$Variable = &$String;
switch($Mode)
{
case 'r':
case 'r+':
$this->fp = fopen('php://memory','r+');
fwrite($this->fp, @strval($String));
rewind($this->fp);
break;
case 'a':
case 'a+':
$this->fp = fopen('php://memory','r+');
fwrite($this->fp, @strval($String));
break;
default:
$this->fp = fopen('php://memory',$Mode);
}
}
final public function flush()
{
# Update variable
$this->Variable = stream_get_contents($this->fp);
}
final public function __destruct()
{
# Update variable on destruction;
$this->Variable = stream_get_contents($this->fp);
}
public function __get($name)
{
switch($name)
{
case 'fp': return $fp;
default: trigger error('Undefined property: ('.$name.').');
}
return NULL;
}
}
$string = 'Some bad-ass string';
$stream = new StringStream($string);
echo stream_get_contents($stream->fp);
#fputcsv($stream->fp, .......);
Run Code Online (Sandbox Code Playgroud)
?>
| 归档时间: |
|
| 查看次数: |
7185 次 |
| 最近记录: |