In PHP, the stream_filter_append() function allows us to attach a filter to a stream, enabling real-time processing and transformation of the data within the stream. This article will show you how to use stream_filter_append() to create a simple text transformation filter that manipulates stream data and performs common text transformations, such as converting text to uppercase or lowercase.
stream_filter_append() is a built-in PHP function that allows you to attach a filter to a stream. Filters process the data flowing through the stream, such as modifying, transforming, or compressing it. Filters can be either built-in or custom-defined.
<span><span><span class="hljs-title function_ invoke__">stream_filter_append</span></span><span>(resource </span><span><span class="hljs-variable">$stream</span></span><span>, </span><span><span class="hljs-keyword">string</span></span> </span><span><span class="hljs-variable">$filtername</span></span>, </span><span><span class="hljs-keyword">int</span></span> </span><span><span class="hljs-variable">$read_write</span></span> = STREAM_FILTER_READ, </span><span><span class="hljs-keyword">mixed</span></span> </span><span><span class="hljs-variable">$params</span></span> = </span><span><span class="hljs-literal">null</span></span>): </span><span><span class="hljs-keyword">bool</span></span><span>
</span></span>
$stream: The stream to which the filter will be attached.
$filtername: The name of the filter, which can be either a built-in filter or a custom-defined filter.
$read_write: Specifies whether the filter applies to reading or writing. STREAM_FILTER_READ applies to reading, while STREAM_FILTER_WRITE applies to writing.
$params: Optional parameters to pass to the filter.
Here is an example of creating a text transformation filter that converts text to uppercase.
First, we need to create a simple filter class that extends the php_user_filter class and implements its methods. This filter will transform the text in the stream to uppercase.
<span><span><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span> </span><span><span class="hljs-title">UppercaseFilter</span></span><span> </span><span><span class="hljs-keyword">extends</span></span><span> </span><span><span class="hljs-title">php_user_filter</span></span><span> {
</span><span><span class="hljs-keyword">public</span></span><span> </span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span> </span><span><span class="hljs-title">filter</span></span>(</span><span><span class="hljs-params"><span class="hljs-variable">$in</span></span></span>, </span><span><span class="hljs-variable">$out</span></span>, &</span><span><span class="hljs-variable">$consumed</span></span>, </span><span><span class="hljs-variable">$closing</span></span>) {
</span><span><span class="hljs-keyword">while</span> (</span><span><span class="hljs-variable">$bucket</span></span> = </span><span><span class="hljs-title function_ invoke__">stream_bucket_make_writeable</span></span>(</span><span><span class="hljs-variable">$in</span></span>)) {
</span><span><span class="hljs-comment">// Convert data in the stream to uppercase</span></span><span>
</span><span><span class="hljs-variable">$bucket</span></span><span>->data = </span><span><span class="hljs-title function_ invoke__">strtoupper</span></span>(</span><span><span class="hljs-variable">$bucket</span></span>->data);
</span><span><span class="hljs-comment">// Output the transformed data to the target stream</span></span><span>
</span><span><span class="hljs-title function_ invoke__">stream_bucket_append</span></span>(</span><span><span class="hljs-variable">$out</span></span>, </span><span><span class="hljs-variable">$bucket</span></span>);
}
</span><span><span class="hljs-keyword">return</span></span> PSFS_PASS_ON;
}
}
</span></span>
In this class, the filter() method is the core part. It receives data from the input stream, converts it to uppercase, and then outputs it to the target stream.
Next, we need to use the stream_filter_register() function to register this custom filter with the PHP environment.
<span><span><span class="hljs-title function_ invoke__">stream_filter_register</span></span><span>(</span><span><span class="hljs-string">"uppercase"</span></span><span>, </span><span><span class="hljs-string">"UppercaseFilter"</span></span><span>) </span><span><span class="hljs-keyword">or</span></span> </span><span><span class="hljs-keyword">die</span></span>(</span><span><span class="hljs-string">"Filter registration failed"</span></span><span>);
</span></span>
This step registers UppercaseFilter under the name uppercase, making it available for use in stream processing.
We can use stream_filter_append() to attach the registered filter to a stream. The example below demonstrates how to read a text file and convert its content to uppercase using a stream filter.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Register the filter</span></span><span>
</span><span><span class="hljs-title function_ invoke__">stream_filter_register</span></span>(</span><span><span class="hljs-string">"uppercase"</span></span>, </span><span><span class="hljs-string">"UppercaseFilter"</span></span>) </span><span><span class="hljs-keyword">or</span></span> </span><span><span class="hljs-keyword">die</span></span>(</span><span><span class="hljs-string">"Filter registration failed"</span></span>);
<p></span>// Open the file stream<br>
$stream = </span>fopen(</span>"example.txt", </span>"r");</p>
<p></span>// Attach the custom transformation filter to the file stream<br>
stream_filter_append(</span>$stream, </span>"uppercase", STREAM_FILTER_READ);</p>
<p></span>// Read and output file content<br>
while ($line = </span>fgets(</span>$stream)) {<br>
</span>echo $line;<br>
}</p>
<p>// Close the file stream<br>
fclose(</span>$stream);<br>
</span>?><br>
</span>
In the example above, we open a file named example.txt and attach the uppercase filter to its stream. Whenever data is read from the stream, the filter automatically converts it to uppercase before outputting it.
Assume the content of example.txt is as follows:
<span><span>hello world
</span><span>this is a test.
</span></span>
Running the above code will produce the output:
<span><span>HELLO WORLD
THIS IS A TEST.
</span></span>
As you can see, all text has been converted to uppercase.
Using stream_filter_append(), we can easily attach filters to streams for real-time data processing. In this article, we created a simple text transformation filter that converts input text to uppercase. The combination of custom filters and streams in PHP provides powerful capabilities for stream data processing, suitable for a variety of complex scenarios.
We hope this simple example helps you understand how to use stream_filter_append() to create your own stream filters and apply them to text processing.