stream_filter_register

Register a user defined stream filter

Syntax

stream_filter_register ( string $filtername , string $classname ) : bool

Parameters

filtername

The filter name to be registered.

classname

To implement a filter, you need to define a class as an extension of php_user_filter with a number of member functions. When performing read/write operations on the stream to which your filter is attached, PHP will pass the data through your filter (and any other filters attached to that stream) so that the data may be modified as desired. You must implement the methods exactly as described in php_user_filter - doing otherwise will lead to undefined behaviour.

Return

Returns TRUE on success or FALSE on failure.

stream_filter_register() will return FALSE if the filtername is already defined.

Examples

1

<?

class strtoupper_filter extends php_user_filter
{
    function filter($in, $out, &$consumed, $closing)
    {
        while ($bucket = stream_bucket_make_writeable($in))
        {
            $bucket->data = strtoupper($bucket->data);
            $consumed += $bucket->datalen;
            stream_bucket_append($out, $bucket);
        }

        return PSFS_PASS_ON;
    }
}

$filtername = "strtoupper";
$classname = "strtoupper_filter";

stream_filter_register($filtername, $classname)
    or exit("failed to register filter");

$filename = "stream_filter_register.txt";
$mode = "w";

$handle = fopen($filename, $mode);

    stream_filter_append($handle, $filtername);

    fwrite($handle, "case");

fclose($handle);

readfile($filename);

?>
CASE

2

<?

class str_filter extends php_user_filter
{
    var $mode;

    function filter($in, $out, &$consumed, $closing)
    {
        while ($bucket = stream_bucket_make_writeable($in))
        {
            if ($this->mode == 0) {
                $bucket->data = strtolower($bucket->data);
            } elseif ($this->mode == 1) {
                $bucket->data = strtoupper($bucket->data);
            }

            $consumed += $bucket->datalen;
            stream_bucket_append($out, $bucket);
        }

        return PSFS_PASS_ON;
    }

    function onCreate()
    {
        if ($this->filtername == "str.tolower") {
            $this->mode = 0;
        } elseif ($this->filtername == "str.toupper") {
            $this->mode = 1;
        } else {
            return false;
        }

        return true;
    }
}

$filtername = "str.*";
$classname = "str_filter";

stream_filter_register($filtername, $classname)
    or exit("failed to register filter");

$filename = "stream_filter_register.txt";
$mode = "w";

$handle = fopen($filename, $mode);

    stream_filter_append($handle, "str.toupper");

    fwrite($handle, "case\n");

    stream_filter_append($handle, "str.tolower");

    fwrite($handle, "CASE");

fclose($handle);

readfile($filename);

?>
CASE
case

PHP

Home Menu