Last active
October 10, 2015 12:38
-
-
Save phpfiddle/3691666 to your computer and use it in GitHub Desktop.
User defined stream filter sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User defined stream filter sample | |
* Source: http://php.net/manual/pt_BR/function.stream-filter-register.php | |
**/ | |
class string_filter extends php_user_filter | |
{ | |
function filter($in, $out, &$consumed, $closing) | |
{ | |
while ($bucket = stream_bucket_make_writeable($in)) | |
{ | |
if ($this->mode == 1) | |
{ | |
$bucket->data = strtoupper($bucket->data); | |
} elseif ($this->mode == 0) | |
{ | |
$bucket->data = strtolower($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 | |
{ | |
return false; | |
} | |
return true; | |
} | |
} | |
stream_filter_register("str.*", "string_filter") || die("Failed to register filter"); // str.toupper, str.tolower | |
$fp = fopen("foo-bar.txt", "w"); | |
stream_filter_append($fp, "str.toupper"); | |
fwrite($fp, "Line1, <br />"); | |
fwrite($fp, "Word - 2, <br />"); | |
fwrite($fp, "Easy As 123. <br />"); | |
fclose($fp); | |
highlight_file("foo-bar.txt"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PhpFiddle.org sample file