Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
User defined stream filter sample
<?php
/**
* User defined stream filter sample
* Source: http://php.net/manual/pt_BR/function.stream-filter-register.php
**/
class string_filter extends php_user_filter
{
function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in))
{
if ($this->mode == 1)
{
$bucket->data = strtoupper($bucket->data);
} elseif ($this->mode == 0)
{
$bucket->data = strtolower($bucket->data);
}
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
function onCreate()
{
if ($this->filtername == 'str.toupper')
{
$this->mode = 1;
} elseif ($this->filtername == 'str.tolower')
{
$this->mode = 0;
} else
{
return false;
}
return true;
}
}
stream_filter_register("str.*", "string_filter") || die("Failed to register filter"); // str.toupper, str.tolower
$fp = fopen("foo-bar.txt", "w");
stream_filter_append($fp, "str.toupper");
fwrite($fp, "Line1, <br />");
fwrite($fp, "Word - 2, <br />");
fwrite($fp, "Easy As 123. <br />");
fclose($fp);
highlight_file("foo-bar.txt");
?>
Owner

PhpFiddle.org sample file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment