<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >stream_filter_register</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Stream Functions" HREF="ref.stream.html"><LINK REL="PREVIOUS" TITLE="stream_filter_prepend" HREF="function.stream-filter-prepend.html"><LINK REL="NEXT" TITLE="stream_filter_remove" HREF="function.stream-filter-remove.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.stream-filter-prepend.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.stream-filter-remove.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.stream-filter-register" ></A >stream_filter_register</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN223017" ></A ><P > (PHP 5)</P >stream_filter_register -- Register a stream filter implemented as a PHP class derived from <TT CLASS="literal" >php_user_filter</TT > </DIV ><DIV CLASS="refsect1" ><A NAME="AEN223021" ></A ><H2 >Description</H2 >bool <B CLASS="methodname" >stream_filter_register</B > ( string filtername, string classname )<BR ></BR ><P > <B CLASS="function" >stream_filter_register()</B > allows you to implement your own filter on any registered stream used with all the other filesystem functions (such as <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A >, <A HREF="function.fread.html" ><B CLASS="function" >fread()</B ></A > etc.). </P ><P > To implement a filter, you need to define a class as an extension of <TT CLASS="literal" >php_user_filter</TT > with a number of member functions as defined below. 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 below - doing otherwise will lead to undefined behaviour. </P ><P > <B CLASS="function" >stream_filter_register()</B > will return <TT CLASS="constant" ><B >FALSE</B ></TT > if the <CODE CLASS="parameter" >filtername</CODE > is already defined. </P >int <B CLASS="methodname" >filter</B > ( resource in, resource out, int &consumed, bool closing )<BR ></BR ><P > This method is called whenever data is read from or written to the attached stream (such as with <A HREF="function.fread.html" ><B CLASS="function" >fread()</B ></A > or <A HREF="function.fwrite.html" ><B CLASS="function" >fwrite()</B ></A >). <CODE CLASS="parameter" >in</CODE > is a resource pointing to a <TT CLASS="literal" >bucket brigade</TT > which contains one or more <TT CLASS="literal" >bucket</TT > objects containing data to be filtered. <CODE CLASS="parameter" >out</CODE > is a resource pointing to a second <TT CLASS="literal" >bucket brigade</TT > into which your modified buckets should be placed. <CODE CLASS="parameter" >consumed</CODE >, which must <SPAN CLASS="emphasis" ><I CLASS="emphasis" >always</I ></SPAN > be declared by reference, should be incremented by the length of the data which your filter reads in and alters. In most cases this means you will increment <CODE CLASS="parameter" >consumed</CODE > by $bucket->datalen for each $bucket. If the stream is in the process of closing (and therefore this is the last pass through the filterchain), the <CODE CLASS="parameter" >closing</CODE > parameter will be set to <TT CLASS="constant" ><B >TRUE</B ></TT > The <CODE CLASS="methodname" >filter</CODE > method must return one of three values upon completion. <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN223071" ></A ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Return Value</TH ><TH >Meaning</TH ></TR ></THEAD ><TBODY ><TR ><TD ><TT CLASS="constant" ><B >PSFS_PASS_ON</B ></TT ></TD ><TD > Filter processed successfully with data available in the <CODE CLASS="parameter" >out</CODE > <TT CLASS="literal" >bucket brigade</TT >. </TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >PSFS_FEED_ME</B ></TT ></TD ><TD > Filter processed successfully, however no data was available to return. More data is required from the stream or prior filter. </TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >PSFS_ERR_FATAL</B ></TT > (default)</TD ><TD > The filter experienced an unrecoverable error and cannot continue. </TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P >bool <B CLASS="methodname" >onCreate</B > ( void )<BR ></BR ><P > 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. Your implementation of this method should return <TT CLASS="constant" ><B >FALSE</B ></TT > on failure, or <TT CLASS="constant" ><B >TRUE</B ></TT > on success. </P ><P > When your filter is first instantiated, and <TT CLASS="literal" >yourfilter->onCreate()</TT > is called, a number of properties will be available as shown in the table below. </P ><P > <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN223102" ></A ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Property</TH ><TH >Contents</TH ></TR ></THEAD ><TBODY ><TR ><TD ><TT CLASS="literal" >FilterClass->filtername</TT ></TD ><TD >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.</TD ></TR ><TR ><TD ><TT CLASS="literal" >FilterClass->params</TT ></TD ><TD >The contents of the <CODE CLASS="parameter" >params</CODE > parameter passed to <A HREF="function.stream-filter-append.html" ><B CLASS="function" >stream_filter_append()</B ></A > or <A HREF="function.stream-filter-prepend.html" ><B CLASS="function" >stream_filter_prepend()</B ></A >.</TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P >void <B CLASS="methodname" >onClose</B > ( void )<BR ></BR ><P > This method is called upon filter shutdown (typically, this is also during stream shutdown), and is executed <SPAN CLASS="emphasis" ><I CLASS="emphasis" >after</I ></SPAN > the <TT CLASS="literal" >flush</TT > method is called. If any resources were allocated or initialzed during <TT CLASS="literal" >onCreate</TT > this would be the time to destroy or dispose of them. </P ><P > The example below implements a filter named <TT CLASS="literal" >strtoupper</TT > on the <TT CLASS="literal" >foo-bar.txt</TT > stream which will capitalize all letter characters written to/read from that stream. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN223131" ></A ><P ><B >例 1. Filter for capitalizing characters on foo-bar.txt stream</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br /></font><font color="#FF8000">/* Define our filter class */<br /></font><font color="#007700">class </font><font color="#0000BB">strtoupper_filter </font><font color="#007700">extends </font><font color="#0000BB">php_user_filter </font><font color="#007700">{<br /> function </font><font color="#0000BB">filter</font><font color="#007700">(</font><font color="#0000BB">$in</font><font color="#007700">, </font><font color="#0000BB">$out</font><font color="#007700">, &</font><font color="#0000BB">$consumed</font><font color="#007700">, </font><font color="#0000BB">$closing</font><font color="#007700">) <br /> {<br /> while (</font><font color="#0000BB">$bucket </font><font color="#007700">= </font><font color="#0000BB">stream_bucket_make_writeable</font><font color="#007700">(</font><font color="#0000BB">$in</font><font color="#007700">)) {<br /> </font><font color="#0000BB">$bucket</font><font color="#007700">-></font><font color="#0000BB">data </font><font color="#007700">= </font><font color="#0000BB">strtoupper</font><font color="#007700">(</font><font color="#0000BB">$bucket</font><font color="#007700">-></font><font color="#0000BB">data</font><font color="#007700">);<br /> </font><font color="#0000BB">$consumed </font><font color="#007700">+= </font><font color="#0000BB">$bucket</font><font color="#007700">-></font><font color="#0000BB">datalen</font><font color="#007700">;<br /> </font><font color="#0000BB">stream_bucket_append</font><font color="#007700">(</font><font color="#0000BB">$out</font><font color="#007700">, </font><font color="#0000BB">$bucket</font><font color="#007700">);<br /> }<br /> return </font><font color="#0000BB">PSFS_PASS_ON</font><font color="#007700">;<br /> }<br />} <br /><br /></font><font color="#FF8000">/* Register our filter with PHP */<br /></font><font color="#0000BB">stream_filter_register</font><font color="#007700">(</font><font color="#DD0000">"strtoupper"</font><font color="#007700">, </font><font color="#DD0000">"strtoupper_filter"</font><font color="#007700">)<br /> or die(</font><font color="#DD0000">"Failed to register filter"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"foo-bar.txt"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Attach the registered filter to the stream just opened */<br /></font><font color="#0000BB">stream_filter_append</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"strtoupper"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"Line1\n"</font><font color="#007700">);<br /></font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"Word - 2\n"</font><font color="#007700">);<br /></font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"Easy As 123\n"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Read the contents back out<br /> */<br /></font><font color="#0000BB">readfile</font><font color="#007700">(</font><font color="#DD0000">"foo-bar.txt"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >LINE1 WORD - 2 EASY AS 123</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN223137" ></A ><P ><B >例 2. Registering a generic filter class to match multiple filter names.</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br /></font><font color="#FF8000">/* Define our filter class */<br /></font><font color="#007700">class </font><font color="#0000BB">string_filter </font><font color="#007700">extends </font><font color="#0000BB">php_user_filter </font><font color="#007700">{<br /> var </font><font color="#0000BB">$mode</font><font color="#007700">;<br /><br /> function </font><font color="#0000BB">filter</font><font color="#007700">(</font><font color="#0000BB">$in</font><font color="#007700">, </font><font color="#0000BB">$out</font><font color="#007700">, &</font><font color="#0000BB">$consumed</font><font color="#007700">, </font><font color="#0000BB">$closing</font><font color="#007700">) <br /> {<br /> while (</font><font color="#0000BB">$bucket </font><font color="#007700">= </font><font color="#0000BB">stream_bucket_make_writeable</font><font color="#007700">(</font><font color="#0000BB">$in</font><font color="#007700">)) {<br /> if (</font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">mode </font><font color="#007700">== </font><font color="#0000BB">1</font><font color="#007700">) {<br /> </font><font color="#0000BB">$bucket</font><font color="#007700">-></font><font color="#0000BB">data </font><font color="#007700">= </font><font color="#0000BB">strtoupper</font><font color="#007700">(</font><font color="#0000BB">$bucket</font><font color="#007700">-></font><font color="#0000BB">data</font><font color="#007700">);<br /> } elseif (</font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">mode </font><font color="#007700">== </font><font color="#0000BB">0</font><font color="#007700">) {<br /> </font><font color="#0000BB">$bucket</font><font color="#007700">-></font><font color="#0000BB">data </font><font color="#007700">= </font><font color="#0000BB">strtolower</font><font color="#007700">(</font><font color="#0000BB">$bucket</font><font color="#007700">-></font><font color="#0000BB">data</font><font color="#007700">);<br /> }<br /><br /> </font><font color="#0000BB">$consumed </font><font color="#007700">+= </font><font color="#0000BB">$bucket</font><font color="#007700">-></font><font color="#0000BB">datalen</font><font color="#007700">;<br /> </font><font color="#0000BB">stream_bucket_append</font><font color="#007700">(</font><font color="#0000BB">$out</font><font color="#007700">, </font><font color="#0000BB">$bucket</font><font color="#007700">);<br /> }<br /> return </font><font color="#0000BB">PSFS_PASS_ON</font><font color="#007700">;<br /> }<br /><br /> function </font><font color="#0000BB">onCreate</font><font color="#007700">() <br /> {<br /> if (</font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">filtername </font><font color="#007700">== </font><font color="#DD0000">'str.toupper'</font><font color="#007700">) {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">mode </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br /> } elseif (</font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">filtername </font><font color="#007700">== </font><font color="#DD0000">'str.tolower'</font><font color="#007700">) {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">mode </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br /> } else {<br /> </font><font color="#FF8000">/* Some other str.* filter was asked for,<br /> report failure so that PHP will keep looking */<br /> </font><font color="#007700">return </font><font color="#0000BB">false</font><font color="#007700">;<br /> }<br /><br /> return </font><font color="#0000BB">true</font><font color="#007700">;<br /> }<br />} <br /><br /></font><font color="#FF8000">/* Register our filter with PHP */<br /></font><font color="#0000BB">stream_filter_register</font><font color="#007700">(</font><font color="#DD0000">"str.*"</font><font color="#007700">, </font><font color="#DD0000">"string_filter"</font><font color="#007700">)<br /> or die(</font><font color="#DD0000">"Failed to register filter"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"foo-bar.txt"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Attach the registered filter to the stream just opened <br /> We could alternately bind to str.tolower here */<br /></font><font color="#0000BB">stream_filter_append</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"str.toupper"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"Line1\n"</font><font color="#007700">);<br /></font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"Word - 2\n"</font><font color="#007700">);<br /></font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#DD0000">"Easy As 123\n"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Read the contents back out<br /> */<br /></font><font color="#0000BB">readfile</font><font color="#007700">(</font><font color="#DD0000">"foo-bar.txt"</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >LINE1 WORD - 2 EASY AS 123</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.stream-wrapper-register.html" ><B CLASS="function" >stream_wrapper_register()</B ></A >, <A HREF="function.stream-filter-prepend.html" ><B CLASS="function" >stream_filter_prepend()</B ></A >, and <A HREF="function.stream-filter-append.html" ><B CLASS="function" >stream_filter_append()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.stream-filter-prepend.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.stream-filter-remove.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >stream_filter_prepend</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.stream.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >stream_filter_remove</TD ></TR ></TABLE ></DIV ></BODY ></HTML >