diff --git a/reference/stream/functions/stream-filter-register.xml b/reference/stream/functions/stream-filter-register.xml index 13bcbad30b..67e9d3bda8 100644 --- a/reference/stream/functions/stream-filter-register.xml +++ b/reference/stream/functions/stream-filter-register.xml @@ -1,5 +1,5 @@ - + stream_filter_register @@ -97,10 +97,42 @@ voidoncreate - + This method is called during instantiation of the filter class object. If your filter allocates or initializes any other resources - (such as a buffer), this is the place to do it. + (such as a buffer), this is the place to do it. Your implementation of + this method should return &false; on failure, or &true; on success. + + + When your filter is first instantiated, and + yourfilter->oncreate() is called, a number of properties + will be available as shown in the table below. + + + + + + + Property + Contents + + + + + FilterClass->filtername + A string containing the name the filter was instantiated with. + Filters may be registered under multiple names or under wildcards. + Use this property to determine which name was used. + + + FilterClass->params + The contents of the params parameter passed + to stream_filter_append + or stream_filter_prepend. + + + + @@ -170,6 +202,82 @@ EASY AS 123 + + + + Registering a generic filter class to match multiple filter names. + +mode == 1) { + $bucket->data = strtoupper($bucket->data); + } elseif ($this->mode == 0) { + $bucket->data = strtoupper($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 { + /* Some other str.* filter was asked for, + report failure so that PHP will keep looking */ + return false; + } + + return true; + } +} + +/* Register our filter with PHP */ +stream_filter_register("str.*", "string_filter") + or die("Failed to register filter"); + +$fp = fopen("foo-bar.txt", "w"); + +/* Attach the registered filter to the stream just opened + We could alternately bind to str.tolower here */ +stream_filter_append($fp, "str.toupper"); + +fwrite($fp, "Line1\n"); +fwrite($fp, "Word - 2\n"); +fwrite($fp, "Easy As 123\n"); + +fclose($fp); + +/* Read the contents back out + */ +readfile("foo-bar.txt"); + +/* Output + * ------ + +LINE1 +WORD - 2 +EASY AS 123 + + */ + +?> +]]> + + + + See Also: stream_wrapper_register,