TOP ▲
itcore TOP
> TIPS
> php_html_update.php
タグ:php html 更新 運用 コンテンツ PHP HTMLの一部を更新する。 | itcore 2019年
■入力ファイル a19_top_rireki.php
<!-- start_001 -->
$arr[20190512][] = "xxx1";
$arr[20190512][] = "xxx2";
$arr[20190511][] = "xxx3";
<!-- end_001 -->
■入力データ
$arr[20190519][] = "yyy1";
$arr[20190519][] = "yyy2";
■出力ファイル a19_top_rireki.php 置換、再実行可能
<!-- 001_start -->
$arr[20190519][] = "yyy1";
$arr[20190519][] = "yyy2";
$arr[20190512][] = "xxx1";
$arr[20190512][] = "xxx2";
$arr[20190511][] = "xxx3";
<!-- 002_end -->
■プログラム
//-----------------------
// 本番移行 実行
//-----------------------
if ("" != uForm("sub_ikou")) {
$yyyymmdd = file_get_contents("$dir/yyyymmdd.txt");
//$new_date = 20190519;
$new_date = $yyyymmdd;
$num = file_get_contents("$dir/num.txt");
$arr = array();
for ($no = 1; $no <= $num; $no++) {
$title = file_get_contents("$dir/title$no.txt");
$arr[$new_date][] = $title;
//echo "debug231 new_date=$new_date no=$no title=".htmlspecialchars2($title)."<br>\n";
}
$file = "../a19_top_rireki.php";
$file_tmp = "../a19_top_rireki.tmp";
//$arr[$new_date][] = "yyy1";
//$arr[$new_date][] = "yyy2";
$data = file_get_contents($file);
if (false === $data) {
echo "<br><font color=red>readエラー file=$file</font><br>\n";;
exit;
}
$ret = file_put_contents($file_tmp, $data);
if (false === $ret) {
echo "<br><font color=red>writeエラー file_tmp=$file_tmp</font><br>\n";;
exit;
}
$a_line = explode("\n", $data);
$start = 0;
$data2 = "";
foreach ($a_line as $line) {
//echo "debug28 line=".htmlspecialchars($line)."<br>\n";
if (1 == $start) {
$last_date = substr($line, 5, 8);
//echo "debug241 last_date=$last_date<br>\n";
if ($last_date == $new_date) {
echo "delete line=".htmlspecialchars($line)."<br>\n";
continue;
}
if ("// @@@ end_001" == $line) {
$start = 2;
}
}
if ("// @@@ start_001" == $line) {
//echo "debug37 line=<font color=red>".htmlspecialchars($line)."</font><br>\n";
foreach ($arr[$new_date] as $text) {
$data2 .= "$line\n";
$line = '$arr['.$new_date.'][]="'.$text.'";';
echo "insert line=".htmlspecialchars($line)."<br>\n";
}
$start = 1;
}
$data2 .= "$line\n";
}
if (0 == $start) {
echo "<br><font color=red>startなし</font><br>\n";;
exit;
}
$data2 = uChomp($data2);
$ret = file_put_contents($file, $data2);
if (false === $ret) {
echo "<br><font color=red>writeエラー file=$file</font><br>\n";;
exit;
}
$file = "$DOCUMENT_ROOT/index_$yyyymmdd"."x.inc";
$html = file_get_contents($file);
$file2 = "$DOCUMENT_ROOT/index_$yyyymmdd".".inc";
file_put_contents($file2, $html);
unlink($file);
echo "<br><font color=darkgreen>本番移行しました!</font><br><br>\n";
}
//-----------------------------
// 共通関数
//-----------------------------
function uForm($var) {
if (isset($_POST[$var])) return $_POST[$var];
if (isset($_GET[$var])) return $_GET[$var];
return "";
}
//行末の改行コードを取り除く。CRLF LF CR
function uChomp($line) {
$cr = chr(13); $lf = chr(10);
$s2 = substr($line, -2);
$s1 = substr($line, -1);
if ("$cr$lf" == $s2) return substr($line, 0, strlen($line) - 2);
if ($lf == $s1) return substr($line, 0, strlen($line) - 1);
if ($cr == $s1) return substr($line, 0, strlen($line) - 1);
return $line;
}