PHP的输出格式化与文件简单处理

PRINTF("THE RESULT IS $%15F\N", 123.42 / 12);
PRINTF("THE RESULT IS $%15.2F\N", 123.42 / 12);

$h = 'House';
printf("[%s]\n", $h); // Standard string output
printf("[%10s]\n", $h); // Right justify with spaces
printf("[%-10s]\n", $h); // Left justify with spaces
printf("[%010s]\n", $h); // Zero padding
printf("[%'#10s]\n\n", $h); // Use the custom padding character '#'
$d = 'Doctor House';
printf("[%10.8s]\n", $d); // Right justify, cutoff of 8 characters
printf("[%-10.6s]\n", $d); // Left justify, cutoff of 6 characters
printf("[%-'@10.6s]\n", $d); // Left justify, pad '@', cutoff 6 chars

$month = 9; // September (only has 30 days)
$day = 30; // 31st
$year = 2012; // 2012
if (checkdate($month, $day, $year)) echo "Date is valid";
else echo "Date is invalid";

$fh = fopen("testfile.txt", 'r') or
die("File does not exist or you lack permission to open it");
$line = fgets($fh);
$text = fread($fh, 3);
fclose($fh);
echo $line;

copy('testfile.txt', 'testfile2.txt') or die("Could not copy file");
if (!rename('testfile2.txt', 'testfile2.new')) echo "Could not rename file";
if (!copy('testfile.txt', 'testfile2.txt')) echo "Could not copy file";
if (!unlink('testfile2.new')) echo "Could not delete file";
echo "File successfully 'testfile2.txt'";

$fh = fopen("testfile.txt", 'r+') or die("Failed to open file");
$text = fgets($fh);
fseek($fh, 0, SEEK_END);
fwrite($fh, "$text") or die("Could not write to file"); // 追加文本
fclose($fh);
echo "File 'testfile.txt' successfully updated";


$fh = fopen("testfile.txt", 'r+') or die("Failed to open file");
$text = fgets($fh);
fseek($fh, 0, SEEK_END);
if (flock($fh, LOCK_EX)) // 给文件上锁以后再写入
{
fwrite($fh, "$text") or die("Could not write to file");
flock($fh, LOCK_UN);
}
fclose($fh);
echo "File 'testfile.txt' successfully updated";

echo "<pre>"; // Enables display of line feeds,使得换行起作用
echo file_get_contents("testfile.txt"); // 显示整个文件的内容
echo "</pre>"; // Terminates pre tag

echo file_get_contents("http://oreilly.com");

$cmd = "dir"; // 执行Windows外部命令
// $cmd = "ls"; // Linux, Unix & Mac
exec(escapeshellcmd($cmd), $output, $status);

if ($status) echo "Exec command failed";
else
{
echo "<pre>";
foreach($output as $line) echo "$line\n";
}

原文地址:https://www.cnblogs.com/findumars/p/2909487.html