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

stream_bucket_append

stream_bucket_make_writeable

stream_bucket_new

stream_bucket_prepend

stream_context_create

stream_context_get_default

stream_context_get_options

stream_context_get_params

stream_context_set_default

stream_context_set_option

stream_context_set_params

stream_copy_to_stream

stream_filter_append

stream_filter_prepend

stream_filter_remove

stream_get_contents

stream_get_filters

stream_get_line

stream_get_meta_data

stream_get_transports

stream_get_wrappers

stream_is_local

stream_isatty

stream_notification_callback

stream_register_wrapper

stream_resolve_include_path

stream_select

stream_set_blocking

stream_set_chunk_size

stream_set_read_buffer

stream_set_timeout

stream_set_write_buffer

stream_socket_accept

stream_socket_client

stream_socket_enable_crypto

stream_socket_get_name

stream_socket_pair

stream_socket_recvfrom

stream_socket_sendto

stream_socket_server

stream_socket_shutdown

stream_supports_lock

stream_wrapper_register

stream_wrapper_restore

stream_wrapper_unregister

PHP

Home Menu