function print_line($format, $args = array(), $where = STDOUT, $terminated_at_line = FALSE) { $args = is_null($args) ? array() : (is_array($args) ? $args : array($args)); vfprintf($where, $format . "\n", $args); if ($terminated_at_line) { die("\n\nExecution terminated at line #" . $terminated_at_line . "\n\n"); } }
function writeAndDump($fp, $format, $args) { ftruncate($fp, 0); $length = vfprintf($fp, $format, $args); rewind($fp); $content = stream_get_contents($fp); var_dump($content); var_dump($length); }
/** * Write some message to the stream, * in a \c printf() fashion. * * \param string $format * A format string to use to send the message. * * \note * You may pass additional parameters to this * method. They will serve as arguments for * the format string. * * \note * You don't need to add an end-of-line sequence * to the format string, one will automatically * be added for you by this method. */ public function write($format) { $args = func_get_args(); array_shift($args); // Protection against format attacks. if (!count($args)) { $args[] = $format; $format = "%s"; } vfprintf($this->stream, $format . PHP_EOL, $args); }
*/ /* * Test vfprintf() when different unsigned formats and unsigned values * are passed to the '$format' and '$args' arguments of the function */ echo "*** Testing vfprintf() : unsigned formats and unsigned values ***\n"; // defining array of unsigned formats $formats = array('%u %+u %-u', '%lu %Lu %4u %-4u', '%10.4u %-10.4u %.4u', '%\'#2u %\'2u %\'$2u %\'_2u', '%3$u %4$u %1$u %2$u'); // Arrays of unsigned values for the format defined in $format. // Each sub array contains unsigned values which correspond to each format string in $format $args_array = array(array(1234567, 01234567, 0), array(12345678900, 12345678900, 1234, 12345), array("1234000", 101234567000.0, 120.0), array(1, 0, 00, "10_"), array(3, 4, 1, 2)); /* creating dumping file */ $data_file = dirname(__FILE__) . '/vfprintf_variation15.txt'; if (!($fp = fopen($data_file, 'wt'))) { return; } // looping to test vfprintf() with different unsigned formats from the above $format array // and with signed and other types of values from the above $args_array array $counter = 1; foreach ($formats as $format) { fprintf($fp, "\n-- Iteration %d --\n", $counter); vfprintf($fp, $format, $args_array[$counter - 1]); $counter++; } fclose($fp); print_r(file_get_contents($data_file)); echo "\n"; unlink($data_file); ?> ===DONE===
/** * @param string[] $argv * @param resource $stdin * @param resource $stdout * @param resource $stderr * @return int UNIX status code */ function printf(array $argv, $stdin = STDIN, $stdout = STDOUT, $stderr = STDERR) { $format = array_shift($argv); if ($format === null) { fwrite($stderr, 'printf: not enough arguments' . PHP_EOL); return 1; } $format = strtr($format, ['\\\\' => '\\', '\\a' => "\\a", '\\b' => "\\b", '\\c' => "\\c", '\\d' => "\\d", '\\e' => "", '\\f' => "\f", '\\g' => "\\g", '\\h' => "\\h", '\\i' => "\\i", '\\j' => "\\j", '\\k' => "\\k", '\\l' => "\\l", '\\m' => "\\m", '\\n' => "\n", '\\o' => "\\o", '\\p' => "\\p", '\\q' => "\\q", '\\r' => "\r", '\\s' => "\\s", '\\t' => "\t", '\\u' => "\\u", '\\v' => "\v", '\\w' => "\\w", '\\x' => "\\x", '\\y' => "\\y", '\\z' => "\\z"]); \vfprintf($stdin, $format, $argv); return 0; }
* are passed to the '$format' and '$args' arguments of the function */ echo "*** Testing vfprintf() : unsigned formats and signed & other types of values ***\n"; // defining array of unsigned formats $formats = '%u %+u %-u %lu %Lu %4u %-4u %10.4u %-10.4u %.4u %\'#2u %\'2u %\'$2u %\'_2u %3$u %4$u %1$u %2$u'; // Arrays of signed and other type of values for the format defined in $format. // Each sub array contains signed values which correspond to each format in $format $args_array = array(array(+2.2, +0.2, +10.2, +123456.234, +123456.234, +1234.6789, +20000000000.0, +2000000000000.0, +22000000000000.0, +12345.78, +12.000000011111, -12.00000111111, -123456.234, +3.33, +4.44, +1.11, -2.22), array(" ", ' ', 'hello', '123hello', "123hello", '-123hello', '+123hello', "S45678hello", "-S45678hello", 'h123456ello', "1234hello", "helloworld", "NULL", "true", "3", "4", '1', '2'), array(array(0), array(1, 2), array(-1, -1), array("123"), array('123'), array('-123'), array("-123"), array(true), array(TRUE), array(FALSE), array("123hello"), array("1", "2"), array('123hello'), array(12 => "12twelve"), array("3"), array("4"), array("1"), array("2")), array(true, TRUE, false, TRUE, 0, FALSE, 1, true, TRUE, FALSE, 0, 1, 1, 0, 1, TRUE, 0, FALSE)); /* creating dumping file */ $data_file = dirname(__FILE__) . '/vfprintf_variation16.txt'; if (!($fp = fopen($data_file, 'wt'))) { return; } // looping to test vfprintf() with different unsigned formats from the above $format array // and with signed and other types of values from the above $args_array array $counter = 1; foreach ($args_array as $args) { fprintf($fp, "\n-- Iteration %d --\n", $counter); vfprintf($fp, $formats, $args); $counter++; } fclose($fp); print_r(file_get_contents($data_file)); echo "\n"; unlink($data_file); ?> ===DONE===
function printf($format, $args = array()) { return vfprintf($this->stream, $format, $args); }
<?php /* Prototype : int vfprintf(resource stream, string format, array args) * Description: Output a formatted string into a stream * Source code: ext/standard/formatted_print.c * Alias to functions: */ // Open handle $file = 'vfprintf_error4.phpt.txt'; $fp = fopen($file, "a+"); echo "\n-- Testing vfprintf() function with other strangeties --\n"; var_dump(vfprintf('foo', 'bar', array('baz'))); var_dump(vfprintf($fp, 'Foo %$c-0202Sd', array(2))); // Close handle fclose($fp); ?> ===DONE=== <?php error_reporting(0); $file = 'vfprintf_error4.phpt.txt'; unlink($file);
/** * @inheritdoc * @see \pharext\Command::error() */ public function error($fmt) { if (!isset($fmt)) { $fmt = "%s\n"; $arg = error_get_last()["message"]; } else { $arg = array_slice(func_get_args(), 1); } vfprintf(STDERR, "ERROR: {$fmt}", $arg); }
<?php /* Prototype : int vfprintf(resource stream, string format, array args) * Description: Output a formatted string into a stream * Source code: ext/standard/formatted_print.c * Alias to functions: */ // Open handle $file = 'vfprintf_error1.phpt.txt'; $fp = fopen($file, "a+"); echo "\n-- Testing vfprintf() function with more than expected no. of arguments --\n"; $format = 'string_val'; $args = array(1, 2); $extra_arg = 10; var_dump(vfprintf($fp, $format, $args, $extra_arg)); var_dump(vfprintf($fp, "Foo %d", array(6), "bar")); // Close handle fclose($fp); ?> ===DONE=== <?php $file = 'vfprintf_error1.phpt.txt'; unlink($file);
// declaring a class class sample { public function __toString() { return "object"; } } // Defining resource $file_handle = fopen(__FILE__, 'r'); //array of values to iterate over $values = array(0, 1, 12345, -2345, 10.5, -10.5, 101234567000.0, 1.07654321E-9, 0.5, NULL, null, true, false, TRUE, FALSE, "", '', "string", 'string', new sample(), @$undefined_var, @$unset_var, $file_handle); /* creating dumping file */ $data_file = dirname(__FILE__) . '/vfprintf_variation21.txt'; if (!($fp = fopen($data_file, 'wt'))) { return; } fprintf($fp, "\n*** Testing vprintf() with unexpected values for args argument ***\n"); $counter = 1; foreach ($values as $value) { fprintf($fp, "\n-- Iteration %d --\n", $counter); vfprintf($fp, $format, $value); $counter++; } fclose($fp); print_r(file_get_contents($data_file)); echo "\n"; unlink($data_file); ?> ===DONE===
* Source code: ext/standard/formatted_print.c */ /* * Testing vfprintf() : basic functionality - using integer format */ echo "*** Testing vfprintf() : basic functionality - using integer format ***\n"; // Initialise all required variables $format = "format"; $format1 = "%d"; $format2 = "%d %d"; $format3 = "%d %d %d"; $arg1 = array(111); $arg2 = array(111, 222); $arg3 = array(111, 222, 333); /* creating dumping file */ $data_file = dirname(__FILE__) . '/vfprintf_basic2.txt'; if (!($fp = fopen($data_file, 'wt'))) { return; } vfprintf($fp, $format1, $arg1); fprintf($fp, "\n"); vfprintf($fp, $format2, $arg2); fprintf($fp, "\n"); vfprintf($fp, $format3, $arg3); fprintf($fp, "\n"); fclose($fp); print_r(file_get_contents($data_file)); unlink($data_file); ?> ===DONE===
<?php /* Prototype : int vfprintf(resource stream, string format, array args) * Description: Output a formatted string into a stream * Source code: ext/standard/formatted_print.c * Alias to functions: */ // Open handle $file = 'vfprintf_error3.phpt.txt'; $fp = fopen($file, "a+"); echo "\n-- Testing vfprintf() function with wrong variable types as argument --\n"; var_dump(vfprintf($fp, array('foo %d', 'bar %s'), 3.55552)); rewind($fp); var_dump(stream_get_contents($fp)); ftruncate($fp, 0); rewind($fp); var_dump(vfprintf($fp, "Foo %y fake", "not available")); rewind($fp); var_dump(stream_get_contents($fp)); ftruncate($fp, 0); rewind($fp); // Close handle fclose($fp); ?> ===DONE=== <?php $file = 'vfprintf_error3.phpt.txt'; unlink($file);
// declaring a class class sample { public function __toString() { return "object"; } } // Defining resource $file_handle = fopen(__FILE__, 'r'); //array of values to iterate over $values = array(0, 1, 12345, -2345, 10.5, -10.5, 101234567000.0, 1.07654321E-9, 0.5, array(), array(0), array(1), array(1, 2), array('color' => 'red', 'item' => 'pen'), NULL, null, true, false, TRUE, FALSE, "", '', new sample(), @$undefined_var, @$unset_var, $file_handle); /* creating dumping file */ $data_file = dirname(__FILE__) . '/vfprintf_variation20.txt'; if (!($fp = fopen($data_file, 'wt'))) { return; } fprintf($fp, "\n*** Testing vprintf() with with unexpected values for format argument ***\n"); $counter = 1; foreach ($values as $value) { fprintf($fp, "\n-- Iteration %d --\n", $counter); vfprintf($fp, $value, $args); $counter++; } fclose($fp); print_r(file_get_contents($data_file)); echo "\n"; unlink($data_file); ?> ===DONE===
function fpg_printf($format = "") { global $fpg_stream; $args = func_get_args(); vfprintf($fpg_stream, $format . PHP_EOL, array_splice($args, 1)); }
<?php /* Prototype : int vfprintf(resource stream, string format, array args) * Description: Output a formatted string into a stream * Source code: ext/standard/formatted_print.c * Alias to functions: */ // Open handle $file = 'vfprintf_error2.phpt.txt'; $fp = fopen($file, "a+"); echo "\n-- Testing vfprintf() function with less than expected no. of arguments --\n"; $format = 'string_val'; var_dump(vfprintf($fp, $format)); var_dump(vfprintf($fp)); var_dump(vfprintf()); // Close handle fclose($fp); ?> ===DONE=== <?php error_reporting(0); $file = 'vfprintf_error2.phpt.txt'; unlink($file);
<?php $tempfile = tempnam('/tmp', 'vmextfiletest'); $f = fopen($tempfile, 'w'); vfprintf($f, "%s %s", array("testing", "vfprintf")); fclose($f); $f = fopen($tempfile, "r"); fpassthru($f); echo "\n"; unlink($tempfile);
/* Prototype : int vfprintf ( resource $handle , string $format , array $args ) * Description: Write a formatted string to a stream * Source code: ext/standard/formatted_print.c */ echo "*** Testing vfprintf() : basic functionality - using string format ***\n"; // Initialise all required variables $format = "format"; $format1 = "%s\n"; $format2 = "%s %s\n"; $format3 = "%s %s %s\n"; $arg1 = array("one"); $arg2 = array("one", "two"); $arg3 = array("one", "two", "three"); /* creating dumping file */ $data_file = dirname(__FILE__) . '/vfprintf_basic1.txt'; if (!($fp = fopen($data_file, 'wt'))) { return; } $result = vfprintf($fp, $format1, $arg1); var_dump($result); $result = vfprintf($fp, $format2, $arg2); var_dump($result); $result = vfprintf($fp, $format3, $arg3); var_dump($result); fclose($fp); print_r(file_get_contents($data_file)); unlink($data_file); ?> ===DONE===
<?php // Various functions that accept resource arguments have different coerce modes // False var_dump('fclose', @fclose(false)); var_dump('pclose', @pclose(false)); var_dump('fseek', @fseek(false, 0)); var_dump('rewind', @rewind(false)); var_dump('ftell', @ftell(false)); var_dump('feof', @feof(false)); var_dump('fstat', @fstat(false)); var_dump('fread', @fread(false, 0)); var_dump('fgetc', @fgetc(false)); var_dump('fgets', @fgets(false)); var_dump('fgetss', @fgetss(false)); var_dump('fscanf', @fscanf(false)); var_dump('fpassthru', @fpassthru(false)); var_dump('fwrite', @fwrite(false, '')); var_dump('fputs', @fputs(false, '')); var_dump('fprintf', @fprintf(false, '')); var_dump('vfprintf', @vfprintf(false, '', array())); var_dump('fflush', @fflush(false)); var_dump('ftruncate', @ftruncate(false)); // NULL var_dump('fputcsv', @fputcsv(false, array())); var_dump('fgetcsv', @fgetcsv(false)); var_dump('flock', @flock(false, 0)); var_dump('readdir', @readdir(false)); var_dump('rewinddir', @rewinddir(false)); var_dump('closedir', @closedir(false));