用流输入php_strip_whitespace

时间:2014-02-11 23:23:11

标签: php

有没有办法将内存流用作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

我也尝试过data:// stream但是我收到了这个错误:

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

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

2 个答案:

答案 0 :(得分: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 :(得分: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
?>