i.i. » docs »

PHP Tips

文字コードを変えて一気に出力する方法 (IDEA * IDEA)
<?php
header('Content-Type: text/html; charset=Shift_JIS');
ob_start();
?>
ここにShift-JISで出力したい内容をUTF-8で記述。
<?php
$output = ob_get_contents();
ob_end_clean();
echo mb_convert_encoding($output, 'SJIS', 'UTF-8');
?>
読み込んで、処理して書き込むまでロックする
<?php
$file = 'count.txt';
if (!file_exists($file)) {
	if (!@touch($file)) {
		exit('Cannot create data file!');
	}
}
$fh = @fopen($file, 'r+');
if (!$fh) {
	exit('Cannot open data file!');
}
flock($fh, LOCK_EX);
$count = (int)fgets($fh);
$count++;

rewind($fh);
if (isset($newcount)) {
	$count = $newcount;
}
// うまくいかないことがある? そんときゃ0で
ftruncate($fh, fwrite($fh, $count));
flock($fh, LOCK_UN);
fclose($fh);

echo $count;
?>

i.i.ishikawa-nct.ac.jp について | お問い合わせ