code2htmltext v1.5 公開

入力したテキストのうち"<"および">"を"<",">"にそれぞれ変換して返します.

  • bodywrap,linewrapによりちょっとした装飾が可能になりました
  • 2つ以上の改行を任意の文字列へ置換できます
  • サンプル
  • ソース
<?php @header('Content-type: text/html;charset=Shift_JIS');

/* script_name: code2htmltext
 * author: sigma
 * author_link: http://sigma.s22.xrea.com/
 * license: The GNU General Public License
 * exposition: 
 *    HTMLフォームに入力された文字列から,
 *    $beforeで設定された文字列群を探しだし,$afterで設定した文字列群へ,
 *    順に変換されます.
 * memo (時間はJST):
 *    2005-09-11 正午前頃: ひととおり完成
 *    2005-09-17 15:10:53 v1.5 変換対象縮小[<>] 実行ボタン併設
 *       空行処理オプションbr化作成 body, line毎のラップ機能
 */

define("PHP_SELF", "code2htmltext.php"); // 自分のファイル名
$before = array("<", ">"); // 変換対象
$after = array("&amp;lt;", "&amp;gt;"); // $beforeに対応
define("TAG_BR", "&lt;br /&gt;"); // br要素の形式
// 変換後データをくくるタグ (option)
define("BODYWRAP_HEAD", "&lt;pre&gt;&lt;code&gt;");
define("BODYWRAP_TAIL", "&lt;/code&gt;&lt;/pre&gt;");
define("LINEWRAP_HEAD", "&lt;l&gt;");
define("LINEWRAP_TAIL", "&lt;/l&gt;");

?>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>code2htmltext</title>
</head>
<body>

<!-- ##### データ表示のフォーム ##### -->
<?php
// ポストデータの存在確認
if(trim($_POST['src'])){
?><form><fieldset><legend>変換後データ表示フォーム</legend>
<textarea><?php 
    //stripslashes()
    $src = stripslashes($_POST['src']);

    // 改行コード整形 -> \n
    str_replace(
        array("\r\n", "\r"),
        array("\r",   "\n"),
        $src
    );
    
    // option_linewrap
    if($_POST['option_linewrap']){
        $src = explode("\n", $src);
        for($i=0; $src[$i]!=NULL; $i++)
            $src[$i] = LINEWRAP_HEAD.trim($src[$i]).LINEWRAP_TAIL;
        $src = implode("\n", $src);
    }
    
    // option_bodywrap
    if($_POST['option_bodywrap'])
        $src = BODYWRAP_HEAD.$src.BODYWRAP_TAIL;
    

    // 3. 空行処理
    switch($_POST['paragraph_output']){
        case "br": // 2つ以上の改行を"<br />\n"に変更すると思われ
             preg_replace("/\n{2,}/", TAG_BR."\n", $src);
            break;
        default:
            // なにもしない
    }
    
    // 出力データ作成
    $src = str_replace($before, $after, $src);
    
    // 表示
    echo $src;
    
?></textarea></fieldset></form>
<?php
}
?>

<!-- ##### データ送信のフォーム ##### -->
<form action="<?php echo PHP_SELF; ?>" method="POST">
<fieldset><legend>データ送信フォーム</legend>
<textarea name="src"></textarea>
<input type="submit"></input>
<fieldset><legend>空行処理</legend>
<input type="radio" id="prg_free" name="paragraph_output" checked value=""></input><label for="prg_free">放置</label><br>
<input type="radio" id="prg_wk2br" name="paragraph_output" value="br"></input><label for="prg_wk2br">空行を"<?php echo TAG_BR; ?>\n"へ置換する</label><br>
</fieldset>
<fieldset><legend>オプション</legend>
<input type="checkbox" id="opt_bodywrap" name="option_bodywrap" checked></input><label for="opt_bodywrap">変換後データを<?echo BODYWRAP_HEAD; ?>内に入れる</label><br>
<input type="checkbox" id="opt_linewrap" name="option_linewrap"></input><label for="opt_linewrap">各行を<?echo LINEWRAP_HEAD; ?>内に入れる</label><br>
</fieldset>
<input type="submit"></input>
</fieldset>
</form>
<div> - <a href="http://sigma.s22.xrea.com/">code2htmltext</a> - </div>
</body>
</html>