2014-02-11 15 views
2

是否有任何方式使用存储流作为输入php_strip_whitespace输入php_strip_whitespace与流

我已经试过这一点,但它不工作,因为PHP://内存每次重新创建一个手柄处于打开状态:

$stream = 'php://memory'; 
    $fp = fopen($stream, 'r+'); 
    fwrite($fp, $this->get_code()); 
    rewind($fp); 

    var_dump(php_strip_whitespace($stream)); -> empty 

我也试过数据://流,但我得到此错误:

php_strip_whitespace(): data:\\ wrapper is disabled in the server configuration by allow_url_include=0 

我想要一个独立于服务器配置的全局解决方案。 谢谢

+1

嗯,我对此表示怀疑,因为你提到的原因。只是定义一个['StreamWrapper'](http://www.php.net/manual/en/class.streamwrapper.php)不是一个选项? – Wrikken

回答

4

最后我得到了这个解决方案,这要归功于Wrikken:

class DummyMemoryStreamWrapper { 

const WRAPPER_NAME = 'var'; 

private static $_content; 
private $_position; 

/** 
* Prepare a new memory stream with the specified content 
* @return string 
*/ 
public static function prepare($content) { 
    if (!in_array(self::WRAPPER_NAME, stream_get_wrappers())) { 
     stream_wrapper_register(self::WRAPPER_NAME, get_class()); 
    } 
    self::$_content = $content; 
} 

public function stream_open($path, $mode, $options, &$opened_path) { 
    $this->_position = 0; 
    return TRUE; 
} 

public function stream_read($count) { 
    $ret = substr(self::$_content, $this->_position, $count); 
    $this->_position += strlen($ret); 
    return $ret; 
} 

public function stream_stat() { 
    return array(); 
} 

public function stream_eof() { 
    return $this->_position >= strlen(self::$_content); 
} 
} 


    $php_code = $this->get_code(); 
    DummyMemoryStreamWrapper::prepare($php_code); 
    $source = php_strip_whitespace(DummyMemoryStreamWrapper::WRAPPER_NAME . '://'); 
1

为什么不只是创建一个临时文件?

<?php 
$temp = tmpfile(); 
fwrite($temp, "writing to tempfile"); 
$info = stream_get_meta_data($temp); 
var_dump(php_strip_whitespace($info['uri'])); 
fclose($temp); // this removes the file 
?> 
+0

这是我现在做的,我想改变的方式 –