Note that stream filters applied to STDOUT are not called when outputting via echo or print.
This is easily demonstrated with the standard ROT13 filter:
<?php
stream_filter_append( STDOUT, "string.rot13" );
print "Hello PHP\n";
// Prints "Hello PHP"
fprintf( STDOUT, "Hello PHP\n" );
// Prints "Uryyb CUC"
?>
If you want to filter STDOUT, you may have better luck with an output buffering callback added via ob_start:
http://php.net/manual/en/function.ob-start.php
At the time of this writing, there is an open PHP feature request to support echo and print for stream filters:
https://bugs.php.net/bug.php?id=30583stream_filter_append
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
stream_filter_append
Референца за `function.stream-filter-append.php` со подобрена типографија и навигација.
stream_filter_append
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
stream_filter_append — Attach a filter to a stream
= NULL
resource
$stream,string
$filter_name,int
$mode = ?,mixed
$params = ?): resource
Додава filter_name Прикачи филтер на поток stream.
Параметри
stream-
Поставете ја големината на делот од протокот.
filter_name-
на списокот на филтри прикачени на
mode-
Стандардно, stream_filter_append() Името на филтерот.
read filter chainќе го прикачи филтерот наrако датотеката беше отворена за читање (т.е. Режим на датотека:+, и/илиwrite filter chain). Филтерот исто така ќе биде прикачен наw,aако датотеката беше отворена за читање (т.е. Режим на датотека:+).STREAM_FILTER_READ,STREAM_FILTER_WRITEако датотеката беше отворена за читање (т.е. Режим на датотека:STREAM_FILTER_ALLако датотеката беше отворена за пишување (т.е. Режим на датотека:modeможе да се помине и на params-
параметар за да се надмине ова однесување.
paramsна end Овој филтер ќе биде додаден со наведениот stream_filter_prepend().
Вратени вредности
од списокот и затоа ќе се повика последен за време на операциите на потокот. За да додадете филтер на почетокот на списокот, користете false Враќа ресурс при успех или
stream_filter_remove().
false се враќа ако stream при неуспех. Ресурсот може да се користи за упатување на оваа инстанца на филтерот за време на повик до filter_name не е ресурс или ако
Примери
не може да се најде.
<?php
/* Open a test file for reading and writing */
$fp = fopen('test.txt', 'w+');
/* Apply the ROT13 filter to the
* write filter chain, but not the
* read filter chain */
stream_filter_append($fp, "string.rot13", STREAM_FILTER_WRITE);
/* Write a simple string to the file
* it will be ROT13 transformed on the
* way out */
fwrite($fp, "This is a test\n");
/* Back up to the beginning of the file */
rewind($fp);
/* Read the contents of the file back out.
* Had the filter been applied to the
* read filter chain as well, we would see
* the text ROT13ed back to its original state */
fpassthru($fp);
fclose($fp);
/* Expected Output
---------------
Guvf vf n grfg
*/
?>Белешки
Забелешка: Пример #1 Контролирање каде се применуваат филтрите
stream_filter_register() Кога користите прилагодени (кориснички) филтриfilter_name.
Забелешка: мора да се повика прво за да се регистрира посакуваниот кориснички филтер на stream_filter_prepend().
Забелешка: Кога се додава филтер за читање и запишување, се создаваат две инстанци на филтерот. stream_filter_append() мора да се повика двапати со
STREAM_FILTER_READandSTREAM_FILTER_WRITEза да се добијат двата ресурси на филтерот.
Види Исто така
- stream_filter_register() - Регистрирај филтер за стрим дефиниран од корисникот
- stream_filter_prepend() Прикачи филтер на стрим
- stream_get_filters() - Преземи листа на регистрирани филтри
Белешки од корисници 2 забелешки
Hello firends
The difference betweem adding a stream filter first or last in the filte list in only the order they will be applied to streams.
For example, if you're reading data from a file, and a given filter is placed in first place with stream_filter_prepend()the data will be processed by that filter first.
This example reads out file data and the filter is applied at the beginning of the reading operation:
<?php
/* Open a test file for reading */
$fp = fopen("test.txt", "r");
/* Apply the ROT13 filter to the
* read filter chain, but not the
* write filter chain */
stream_filter_prepend($fp, "string.rot13",
STREAM_FILTER_READ);
// read file data
$contents=fread($fp,1024);
// file data is first filtered and stored in $contents
echo $contents;
fclose($fp);
?>
On the other hand, if stream_filter_append() is used, then the filter will be applied at the end of the data operation. The thing about this is only the order filters are applied to streams. Back to the example, it's not the same thing removing new lines from file data and then counting the number of characters, than performing the inverse process. In this case, the order that filters are applied to stream is important.
This example writes a test string to a file. The filter is applied at the end of the writing operation:
<?php
/* Open a test file for writing */
$fp = fopen("test.txt", "w+");
/* Apply the ROT13 filter to the
* write filter chain, but not the
* read filter chain */
stream_filter_append($fp, "string.rot13",
STREAM_FILTER_WRITE);
/* Write a simple string to the file
* it will be ROT13 transformed at the end of the
stream operation
* way out */
fwrite($fp, "This is a test\n"); // string data is
first written, then ROT13 tranformed and lastly
written to file
/* Back up to the beginning of the file */
rewind($fp);
$contents=fread($fp,512);
fclose($fp);
echo $contents;
?>
In the first case, data is transformed at the end of the writing operation, while in the second one, data is first filtered and then stored in $contents.
With Regards
Hossein