note 59674 added to function.stream-filter-append

From: Date: Tue, 13 Dec 2005 06:47:00 +0000
Subject: note 59674 added to function.stream-filter-append
Groups: php.notes 
Request: Send a blank email to php-notes+get-100045@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.stream-filter-append.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59674 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59674&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59674&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59674&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59674&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59674&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59674&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59674&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59674&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100045) next »