文章中の文字列を別の文字列に一括変換
#! /usr/local/bin/perl
# フォームデータ取り込み
read(STDIN, $query, $ENV{'CONTENT_LENGTH'});
foreach $pair (split(/&/, $query)) {
($key, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$FORM{$key} = $value;
}
# 変換
if($FORM{'word1'}) {
$FORM{'text'} =~ s/$FORM{'word1'}/$FORM{'word2'}/g;
}
if($FORM{'word3'}) {
$FORM{'text'} =~ s/$FORM{'word3'}/$FORM{'word4'}/g;
}
if($FORM{'word5'}) {
$FORM{'text'} =~ s/$FORM{'word5'}/$FORM{'word6'}/g;
}
$FORM{'text'} =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg;
# HTML出力
print qq(Content-type: text/html; charset=Shift_JIS\n\n);
print <<END;
<html>
<head>
<title>文章中の文字列を別の文字列に一括変換</title>
<script language="JavaScript">
<!--
function baby() {
document.form.word1.value = "です";
document.form.word2.value = "でちゅ";
document.form.word3.value = "ます";
document.form.word4.value = "まちゅ";
}
//-->
</script>
</head>
<body>
<form name="form" action="$ENV{'SCRIPT_NAME'}" method="POST">
<h1>文章中の文字列を別の文字列に一括変換</h1>
<textarea name="text" rows="20" cols="70">$FORM{'text'}</textarea><br>
<br>
文章中の変換したい文字列を入力してください。
(例:<a href="javascript:baby()">赤ちゃん言葉に変換</a>)<br>
<input type="text" name="word1" value="" size="50"> ⇒
<input type="text" name="word2" value="" size="50"><br>
<input type="text" name="word3" value="" size="50"> ⇒
<input type="text" name="word4" value="" size="50"><br>
<input type="text" name="word5" value="" size="50"> ⇒
<input type="text" name="word6" value="" size="50"><br>
<input type="submit" value=" 変 換 ">
</form>
</body>
</html>
END
exit;
〔 実行する 〕