Skip to content

Instantly share code, notes, and snippets.

@wotupset
Last active December 28, 2015 17:09
Show Gist options
  • Save wotupset/7533494 to your computer and use it in GitHub Desktop.
Save wotupset/7533494 to your computer and use it in GitHub Desktop.
131119 讀寫檔案練習
<?php
/*
http://www.php.net/manual/de/function.eval.php#59862
压缩函数:gzcompress gzdeflate /gzencode(not always installed)
解压函数:gzuncompress gzinflate /gzdecode(not always installed)
base64_encode <=> base64_decode
htmlspecialchars
*/
//*****************
header('Content-type: text/html; charset=utf-8');
$phpself=basename($_SERVER["SCRIPT_FILENAME"]);//被執行的文件檔名
extract($_POST,EXTR_SKIP);
//*****************
$form=<<<EOT
<form action="./$phpself" method="post" id='form1' onsubmit="return check2();">
<input type="hidden" name="mode" value="reg">
內文<textarea name="celltext" id="celltext" cols="48" rows="4" wrap=soft></textarea>
<br/>
<select name="arg1" onchange="">
<option value="encode" selected="selected">加密</option>
<option value="decode">解密</option>
</select>
<span style="display:none;">
<input type="checkbox" id="chk" name="chk">
<input type="text" name="na" id="na" size="8" value="??">
</span>
<input type="submit" id='send' name="send" value="送出"/>
<h2>$ver</h2>
<a href='../'>../</a>
</form>
<script language="Javascript">
document.getElementById("na").value="3";
document.getElementById("chk").checked=true;
function check(){document.getElementById("send").value="稍後";}
function check2(){
document.getElementById("send").disabled=true;
document.getElementById("send").style.backgroundColor="#ff0000";
var tmp="";
var regStr = 'http://';
var re = new RegExp(regStr,'gi');
tmp = document.getElementById("celltext").value;
//alert(regStr);
tmp = tmp.replace(re,"tttpp//");//有些免空會擋過多的http字串
document.getElementById("celltext").value =tmp;
}
</script>
EOT;
//*****************
$htmlend=<<<EOT
\n<br/>
eval_gzinflate_base64_decode\n<br/>
echo_base64_encode_gzdeflate\n<br/>
</body></html>
EOT;
//*****************
$htmlhead=<<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="zh-tw">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META http-equiv="Content-Script-Type" content="text/javascript">
<META http-equiv="Content-Style-Type" content="text/css">
<STYLE TYPE="text/css"><!--
body,pre { font-family:'細明體','MingLiU';}
--></STYLE>
<title></title>
</head>
<body>
EOT;
//*****************
switch($mode){
case 'reg':
$cell=$celltext;
$cell = preg_replace("/\r\n/","\n",$cell);
$cell = preg_replace("/http\:\/\//", "ttpp//", $cell);//
$cell = preg_replace("/ttpp\/\//", "http://", $cell);//有些免空會擋過多的http字串
$celltext=$cell;
switch($arg1){//加密
case 'encode':
//檢查是否支援gzip
if(!function_exists("gzdeflate")){die('不支援gzdeflate');}else{}
if(get_magic_quotes_gpc()) {$celltext=stripcslashes($celltext);}//去掉伺服器自動加的反斜線
$celltext=base64_encode(gzdeflate($celltext));
//$celltext="eval(gzinflate(base64_decode('".$celltext."')));";
$celltext=chunk_split($celltext,77,"\n");
break;
case 'decode':
$celltext=gzinflate(base64_decode($celltext));
break;
default:
die('switch error');
break;
}
$htmlbody="<pre>".$celltext."</pre>";
echo $htmlhead;
echo $form;
echo $htmlbody;
echo $htmlend;
break;
default:
echo $htmlhead;
echo $form;
echo $htmlend;
break;
}
//*****************
?>
<?php
header("content-Type: text/html; charset=utf-8"); //語言強制
error_reporting(E_ALL & ~E_NOTICE); //所有錯誤中排除NOTICE提示
$phpself=basename($_SERVER["SCRIPT_FILENAME"]);//被執行的文件檔名
date_default_timezone_set("Asia/Taipei");//時區設定
$time=time();
$mode = $_POST["mode"];
$file_org = $_POST["file_org"];
$chg = $_POST["chg"];
$arg2 = $_POST["arg2"];
$pw2 = $_POST["pw2"];
$password='qqq';
//setcookie("cookie_file_org", $file_org,$time+7*24*3600); //存入原始的密碼 7天過期
//$cookie_file_org=$_COOKIE["cookie_file_org"];
session_start(); //啟用session功能
//有session 就直接使用
if(isset($_SESSION['session_pw'])){$session_pw=$_SESSION['session_pw'];}
//**************
$htmlhead=<<<EOT
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>gzip</title>
<style>
body {font-family:'細明體','MingLiU';}
</style>
</head><body>
EOT;
//**************
$htmlend=<<<EOT
</body></html>
EOT;
//**************
$form=<<<EOT
<form id='form1' action='$phpself' method="post" autocomplete="off">
<input type="hidden" name="mode" value="reg">
<input type="text" name="file_org" id="file_org" size="20" placeholder="no session" value="$session_file_org">
<select name="chg" onchange="">
<option value="txt">txt</option>
<option value="htm">htm</option>
<option value="php">php</option>
<option value="log">log</option>
<option value="txt" selected="selected">輸出成</option>
</select>
<select name="arg2" onchange="">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="1" selected="selected">次數</option>
</select>
<input type="text" name="pw2" id="pw2" size="10" placeholder="pw2" value="$session_pw">
<input type="submit" value="送出"/>
</form>
<a href='./'>目錄</a><br/>
<br/>
EOT;
//**************
//&#10004; = HTML Entities
//ob_end_flush();
//ob_start('ob_gzhandler');//攔截ehco的輸出內容
ob_start();
//**************
switch($mode){
//**************
case 'reg':
$_SESSION['session_file_org']=$file_org;
$session_file_org=$_SESSION['session_file_org'];
$_SESSION['session_pw']=$pw2;
if(isset($_SESSION['session_pw'])){
echo "&#10004;session ok<br/>\n";
}else{
echo "&#10006;session no work<br/>\n";
}
$session_pw=$_SESSION['session_pw'];
if($session_pw==$password){}else{die('xpw');}
//清理輸入的檔名
if($file_org){
$file_org=trim($file_org);
echo "&#10004;清理輸入的檔名 ok<br/>\n";
}else{
$file_org='index.php';
echo "&#10006;沒輸入檔名 改用index.php<br/>\n";
}
//檢查檔案是否存在
if(is_file($file_org)){ //file_exists
echo "&#10004;檔案存在<br/>\n";
}else{
$file_org=$phpself;
echo "&#10006;檔案不存在 改用$phpself<br/>\n";
}
//讀取檔案內容
$tmp_f=file_get_contents($file_org);
$tmp_f=trim($tmp_f);
$tmp_f_ext=substr($file_org,-3);
//是否支援gzdeflate
if(1){//註解
if(function_exists("gzdeflate")){//支援gzdeflate
switch($tmp_f_ext){
case 'htm':
//**************
$tmp_f="
echo <<<EOT
$tmp_f
EOT;
";
break;
case 'php':
//**************
$tmp_f=trim($tmp_f);
$tmp_f=preg_replace("/^<\?php/","",$tmp_f);
$tmp_f=preg_replace("/\?>$/","",$tmp_f);
break;
default:
//**************
$tmp_f="
echo <<<EOT
$tmp_f
EOT;
";
break;
}
if($arg2>0){//正數=加密
$cc=0;
for($i=0 ;$i<$arg2 ;$i++){//加密圈數
$cc=$cc+1;
$tmp_f=base64_encode(gzdeflate($tmp_f));
$tmp_f=chunk_split($tmp_f,77,"\n"); //切割字串
$tmp_f=trim($tmp_f);
$tmp_f="/*circle $cc*/\neval( gzinflate( base64_decode('".$tmp_f."')));";
}
}else{//零=不加密
$tmp_f=trim($tmp_f);
}
//$tmp_f=chunk_split($tmp_f,77,"\n"); //切割字串
$tmp_f=trim($tmp_f);
$tmp_f="<?php\n".$tmp_f."\n?>";
echo "&#10004;支援gzdeflate<br/>\n";
}else{//不支援gzdeflate
$tmp_f=$tmp_f;
echo "&#10006;不支援gzdeflate<br/>\n";
}
}
$file_org2=substr($file_org,0,strpos($file_org,".",0));
//要輸出的檔案名稱
$output_f="./tmp_$file_org2.$chg";
if(file_exists($output_f)){unlink($output_f);}
$cp = fopen($output_f, "a+") or die('failed');// 讀寫模式, 指標於最後, 找不到會嘗試建立檔案
rewind($cp); //從頭讀取
ftruncate($cp, 0); //砍資料至0
fputs($cp, $tmp_f);
fclose($cp);
$filesize1=filesize($file_org);
$filesize2=filesize($output_f);
$pp=floor(($filesize2/$filesize1)*100);
echo "<br/>\n輸出$file_org 類型$tmp_f_ext->$chg 大小$filesize1->$filesize2 壓縮率$pp% 層次$arg2<br/>\n";
echo "<a href='./$output_f?$time'>$output_f?$time</a><br/>\n";
//
$echo_body=ob_get_contents();//輸出擷取到的echo
break;
//**************
default:
$echo_body='';
/*
压缩函数:gzcompress gzdeflate /gzencode(not always installed)
解压函数:gzuncompress gzinflate /gzdecode(not always installed)
base64_encode <=> base64_decode
*/
if(function_exists("gzcompress")){echo "&#10004;";}else{echo "&#10006;";}echo "gzcompress";
if(function_exists("gzuncompress")){echo "&#10004;";}else{echo "&#10006;";}echo "gzcompress";
echo "<br/>\n";
if(function_exists("gzdeflate")){echo "&#10004;";}else{echo "&#10006;";}echo "gzdeflate";
if(function_exists("gzinflate")){echo "&#10004;";}else{echo "&#10006;";}echo "gzinflate";
echo "<br/>\n";
if(function_exists("gzencode")){echo "&#10004;";}else{echo "&#10006;";}echo "gzencode";
if(function_exists("gzdecode")){echo "&#10004;";}else{echo "&#10006;";}echo "gzdecode";
echo "<br/>\n";
$echo_body=ob_get_contents();//輸出擷取到的echo
break;
}
ob_end_clean();//清空擷取到的內容
echo $htmlhead;
echo $form;
echo $echo_body;
echo $htmlend;
?>
<?php
header("content-Type: text/html; charset=utf-8"); //語言強制
error_reporting(E_ALL & ~E_NOTICE); //所有錯誤中排除NOTICE提示
$phpself=basename($_SERVER["SCRIPT_FILENAME"]);//被執行的文件檔名
date_default_timezone_set("Asia/Taipei");//時區設定
$time=time();
$password='qqq';
extract($_POST,EXTR_SKIP);
$title=chra_fix($title);
//setcookie("cookie_file_org", $file_org,$time+7*24*3600); //存入原始的密碼 7天過期
//$cookie_file_org=$_COOKIE["cookie_file_org"];
session_cache_expire(30);/* set the cache expire to 30 minutes */
session_cache_limiter('private'); //不清空表單,只在session生效期間
session_start(); //啟用session功能
$_SESSION['session_on']="on";
//$session_on=$_SESSION['session_on'];
$_SESSION['session_pw']=$pw2;
$session_pw=$_SESSION['session_pw'];
//**************
$htmlhead=<<<EOT
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>gzip</title>
<style>
body {font-family:'細明體','MingLiU';}
</style>
</head><body>
EOT;
//**************
$htmlend=<<<EOT
</body></html>
EOT;
//**************
$fyy=gmdate("Y",$time);
$fmm=gmdate("m",$time);
$fdd=gmdate("d",$time);
$form=<<<EOT
<form id='form1' action='$phpself' method="post" autocomplete="off">
<input type="hidden" name="mode" value="reg">
<input type="text" name="yy" id="yy" size="10" placeholder="1970" value="$fyy">
<input type="text" name="mm" id="mm" size="10" placeholder="01" value="$fmm">
<input type="text" name="dd" id="dd" size="10" placeholder="01" value="$fdd">
<input type="text" name="title" id="title" size="10" placeholder="標題" value="">
<input type="text" name="pw2" id="pw2" size="10" placeholder="pw2" value="$session_pw">
<br>
<label><input type="checkbox" name="backup1">載入還原資料</label>
<label><input type="text" name="backup2" size="20" placeholder="csv" value="">檔名</label>
<input type="submit" value="送出"/>
</form>
<a href='./'>目錄</a><br/>
<br/>
EOT;
//**************
//&#10004; = HTML Entities
//ob_end_flush();
ob_start();//攔截ehco的輸出內容
//**************
switch($mode){
//**************
case 'reg':
if($session_pw==$password){}else{die('xpw');}
echo "輸入的內容 $yy-$mm-$dd, $title, $pw2<br/>\n";
$input='';
if($yy &&$mm &&$dd &&$title &&$pw2){
$input="$yy-$mm-$dd,$title";
$tmp_str=md5($input);//計算md5
$input=$tmp_str.",".$input;//要插入的資料
echo "資料完整 準備寫入<br/>\n";
}else{
$input="";
echo "資料不完整 擱置作業<br/>\n";
}
if($backup1 && !$title){//有勾選+沒標題 使用外部備份資料
$backup2=trim($backup2);
if(is_file($backup2)){//檔案存在
$file_org="./$backup2";//資料來源
}else{
$file_org="./jph.txt";//資料來源
}
}else{
$file_org="./jph.txt";//資料來源
}
//$file_org="./jpHanime.txt";
if(is_file($file_org)){}else{die('xf');}//檢查檔案是否存在
@chmod($file_org,0777);
$file_org=trim($file_org);
$tmp_f=file_get_contents($file_org);//讀取檔案內容
$tmp_f=trim($tmp_f);//讀取出來的資料
//****************
echo "<br/>\n<pre>\n";
if($backup1 && !$title){//有勾選+沒標題 使用外部備份資料
//來自csv資料
$tmp_f=explode("\n",$tmp_f);//分割成陣列
if($input){$tmp_f=array_unshift($tmp_f,$input);}//在陣列前面加上新資料
$title=array();$item_cc=0;
foreach ($tmp_f as $key => $v) { //來自csv資料
$tmp_f2=explode(",",$tmp_f[$key]); // A,B => array(A,B);
$date[$key] = $tmp_f2[0];
$title[$key] = $tmp_f2[1];
$md5[$key] = $date[$key].",".$title[$key];
$md5[$key] = md5($md5[$key]); //計算md5 //sprintf("%1\$-.12s", );
$data[] = array('md5' => $tmp_f2[0], 'date' => $tmp_f2[1],'title' => $tmp_f2[2]);
echo $md5[$key].",".$date[$key].",".$title[$key]."\n";
$item_cc=$item_cc+1;
}
}else{//正常處理
//正常處理
$tmp_f=explode("\n",$tmp_f);//分割
if($input){$tmp_f=array_unshift($tmp_f,$input);}//在陣列前面加上新資料
$title=array();$item_cc=0;
foreach ($tmp_f as $key => $v) {//正常處理
$tmp_f2=explode(",",$tmp_f[$key]); // md5,A,B => array(A,B);
$md5[$key] = $tmp_f2[0];
$date[$key] = $tmp_f2[1];
$title[$key] = $tmp_f2[2];
$data[] = array('md5' => $tmp_f2[0], 'date' => $tmp_f2[1],'title' => $tmp_f2[2]);
echo $md5[$key].",".$date[$key].",".$title[$key]."\n";
$item_cc=$item_cc+1;
}
}
//****************
array_multisort($date, SORT_DESC,SORT_STRING,
$title, SORT_DESC,SORT_STRING,
$md5, SORT_DESC,SORT_STRING,
$data);
//****************
$item_cc=0;
foreach ($tmp_f as $key => $v) {
$item_cc=$item_cc+1;
$tmp_f[$key]=$date[$key].",".$title[$key];
//$tmp_str=sprintf("%1\$-.12s", md5($tmp_f[$key]));//使用第一個參數 只取倒數8字元
$tmp_str=md5($tmp_f[$key]);
if($tmp_str==$md5[$key]){//比較md5有無錯誤
echo "row $item_cc: ok\n";
}else{
echo "row $item_cc: md5錯誤 $tmp_str , $md5[$key]\n";
}
$tmp_f[$key]=$tmp_str.",".$tmp_f[$key]; //合成資料
}
echo "\n</pre>\n";
$tmp_f=implode("\n",$tmp_f);//把陣列集合成字串
//**************
//$file_org2=substr($file_org,0,strpos($file_org,".",0));
//要輸出的檔案名稱
$output_f="./jph.txt";
if(file_exists($output_f)){unlink($output_f);}
$cp = fopen($output_f, "a+") or die('failed');// 讀寫模式, 指標於最後, 找不到會嘗試建立檔案
rewind($cp); //從頭讀取
ftruncate($cp, 0); //砍資料至0
fputs($cp, $tmp_f);
fclose($cp);
@chmod($output_f,0777);
//$filesize1=filesize($file_org);
//$filesize2=filesize($output_f);
//$pp=floor(($filesize2/$filesize1)*100);
//echo "<br/>\n輸出$file_org 類型$tmp_f_ext 大小$filesize1->$filesize2 壓縮率$pp% 層次$arg2<br/>\n";
if($input){//有輸入資料
echo "項目$item_cc +1<br/>\n";
}else{//沒輸入資料
echo "項目$item_cc +0<br/>\n";
}
//
$file_link="<a href='./$output_f?$time'>$output_f?$time</a><br/>\n";
$echo_body=ob_get_contents();//輸出擷取到的echo
$echo_body=$file_link.$echo_body;//連結放上面
break;
//**************
default:
$echo_body='';
/*
压缩函数:gzcompress gzdeflate /gzencode(not always installed)
解压函数:gzuncompress gzinflate /gzdecode(not always installed)
base64_encode <=> base64_decode
*/
if(function_exists("gzcompress")){echo "&#10004;";}else{echo "&#10006;";}echo "gzcompress";
if(function_exists("gzuncompress")){echo "&#10004;";}else{echo "&#10006;";}echo "gzcompress";
echo "<br/>\n";
if(function_exists("gzdeflate")){echo "&#10004;";}else{echo "&#10006;";}echo "gzdeflate";
if(function_exists("gzinflate")){echo "&#10004;";}else{echo "&#10006;";}echo "gzinflate";
echo "<br/>\n";
if(function_exists("gzencode")){echo "&#10004;";}else{echo "&#10006;";}echo "gzencode";
if(function_exists("gzdecode")){echo "&#10004;";}else{echo "&#10006;";}echo "gzdecode";
echo "<br/>\n";
if(isset($_SESSION['session_on'])){echo "&#10004;";}else{echo "&#10006;";}echo "session";
echo "<br/>\n";
$echo_body=ob_get_contents();//輸出擷取到的echo
break;
}
//$echo_body=ob_get_contents();//輸出擷取到的echo
ob_end_clean();//清空擷取到的內容
function chra_fix($tmp_xx){
$tmp_xx=trim($tmp_xx);
//$w=addslashes($tmp_xx);//跳脫字元
if(get_magic_quotes_gpc()) {$tmp_xx=stripcslashes($tmp_xx);}//去掉伺服器自動加的反斜線
$tmp_xx=htmlspecialchars($tmp_xx);//HTML特殊字元
// &->&amp; "->&quot; '->&#039; <->&lt; >->&gt;
$tmp_xx=str_replace("\r\n", "\r", $tmp_xx); //改行文字の統一。
$tmp_xx=str_replace("\r", "\n",$tmp_xx);//Enter符->換行符
$tmp_xx=str_replace(" ", " ",$tmp_xx);//全形空格
$tmp_xx=preg_replace("/[\n]+/","<br/>",$tmp_xx);//換行符 改成<br/>
$tmp_xx=preg_replace("/[\s]+/"," ",$tmp_xx);//等價於[\f\n\r\t\v]多個空白 換成一個
//$tmp_xx=str_replace("\t", " ",$tmp_xx);//水平製表符
//$tmp_xx=preg_replace("/\v/"," ",$tmp_xx);//垂直製表符
//$tmp_xx=preg_replace("/\f/"," ",$tmp_xx);//換頁符
//$tmp_xx=preg_replace("/\s/","",$tmp_xx);//
$tmp_xx=str_replace(",", "&#44;",$tmp_xx);//逗號
$tmp_xx=str_replace('\"', '&#34;', $tmp_xx);//雙引號 換成 HTML Characters
$tmp_xx=str_replace('\'', '&#39;', $tmp_xx);//單引號 換成 HTML Characters
$tmp_xx=str_replace('$', '&#36;', $tmp_xx);//錢字號 換成 HTML Characters
$tmp_xx=str_replace('*', '&#42;', $tmp_xx);//米字號 換成 HTML Characters
$tmp_xx=str_replace('^', '&#94;', $tmp_xx);//插入符 換成 HTML Characters
$tmp_xx=str_replace('\\', '&#92;', $tmp_xx);//backslash 換成 HTML Characters
//$tmp_xx=str_replace('/', '&#47;', $tmp_xx);//backslash 換成 HTML Characters
$tmp_xx=str_replace('+', '&#43;', $tmp_xx);//加號 換成 HTML Characters
$tmp_xx=str_replace('?', '&#63;', $tmp_xx);//問號 換成 HTML Characters
//$tmp_xx=str_replace("=", "&#61;", $tmp_xx); //等於 換成 HTML Characters
//$tmp_xx=str_replace("\\", "&#92;", $tmp_xx);
return $tmp_xx;
}
echo $htmlhead;
echo $form;
echo $echo_body;
echo $htmlend;
?>
<?php
//echo chunk_split($str,1,".");
header('Content-type: text/html; charset=utf-8');
$phpself=basename($_SERVER["SCRIPT_FILENAME"]);//被執行的文件檔名
error_reporting(E_ALL & ~E_NOTICE); //所有錯誤中排除NOTICE提示
extract($_POST,EXTR_SKIP);
extract($_GET,EXTR_SKIP);
extract($_COOKIE,EXTR_SKIP);
$htmlhead=<<<EOT
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>c84cosplay</title>
</head><body>
EOT;
$htmlend=<<<EOT
</body></html>
EOT;
$form=<<<EOT
<form id='form1' action='$phpself' method="post" autocomplete="off">
<input type="hidden" name="mode" value="reg">
<input type="text" name="str_org" id="str_org" size="20" placeholder="字串" value="">
<input type="submit" value="送出"/>
</form>
EOT;
$htmlbody='';
$htmlbody.=$form;
switch($mode){
case 'reg':
echo $str_org.'<br/>'.cstrrev($str_org);
break;
default:
break;
}
$htmlbody.="<a href='./'>ROOT</a> <a href='./$phpself'>BACK</a>";
echo $htmlhead;
echo $htmlbody;
echo $htmlend;
function cstrrev($str){
$len = strlen($str);
$cc=0;
for($i = 0; $i < $len; $i++){
$char = $str{0}; //取字串的第一個字元
if(ord($char) > 127){//檢查是否非字母
if($i < $len){
$arr[$cc][0] = substr($str, 0, 3); //前3字元 切割存到陣列
$arr[$cc][1] = "3";
$str = substr($str, 3); //切割過的移除
}
}else{
$arr[$cc][0] = $char; //前1字元 切割存到陣列
$arr[$cc][1] = "1";
$str = substr($str, 1); //切割過的移除
}
$cc=$cc+1;
}
$str='';
foreach ($arr as $key => $v) { //來自csv資料
switch($arr[$key][1]){
case 1:
$str.=$arr[$key][0];
break;
case 3:
$str.=$arr[$key][0]."w";
break;
default:
break;
}
}
return $str;
}
?>
<?php
header('Content-type: text/html; charset=utf-8');
$phpself=basename($_SERVER["SCRIPT_FILENAME"]);//被執行的文件檔名
error_reporting(E_ALL & ~E_NOTICE); //所有錯誤中排除NOTICE提示
extract($_POST,EXTR_SKIP);
extract($_GET,EXTR_SKIP);
extract($_COOKIE,EXTR_SKIP);
$htmlhead=<<<EOT
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>c84cosplay</title>
</head><body>
EOT;
$htmlend=<<<EOT
</body></html>
EOT;
$htmlbody='';
$form=<<<EOT
<form id='form1' action='$phpself' method="post" autocomplete="off">
<input type="hidden" name="mode" value="reg">
<input type="text" name="str_org" id="str_org" size="20" placeholder="反轉字串" value="">
<input type="submit" value="送出"/>
</form>
EOT;
$htmlbody.=$form;
switch($mode){
case 'reg':
echo $str_org.'<br/>'.cstrrev($str_org);
break;
default:
break;
}
$htmlbody.="<a href='./'>ROOT</a> <a href='./$phpself'>BACK</a>";
echo $htmlhead;
echo $htmlbody;
echo $htmlend;
function cstrrev($str){
$len = strlen($str);
for($i = 0; $i < $len; $i++){
$char = $str{0};
if(ord($char) > 127){
$i++;
if($i < $len){
$arr[] = substr($str, 0, 3);
$str = substr($str, 3);
}
}else{
$tmp=ord($char);
switch($tmp){
case '40':
$char=chr(41);
break;
case '41':
$char=chr(40);
break;
case '91':
$char=chr(93);
break;
case '93':
$char=chr(91);
break;
}
$arr[] = $char;
$str = substr($str, 1);
}
}
$str=join(array_reverse($arr));
return $str;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment