<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>base64 encode test 입니다 :)</title>
</head>
<body>
<form method='post' action='test.php'>
<textarea name='test'></textarea>
Debug : <input type="checkbox" name="debug">
<input type='submit'>
</form>
<?PHP
function utf8_validation( $str, &$i ){
$i = 0;
$len = strlen($str);
while( $i < $len ){
if( (ord($str[$i]) & 0xF0) == 0xE0 ){
if( $i <= ($len-3) &&
(ord($str[$i+1])&0x80) == 0x80 &&
(ord($str[$i+2])&0x80) == 0x80 )
$i += 3;
else
return;
}
// 2Byte
else if( (ord($str[$i]) & 0xE0) == 0xC0 ){
if( $i <= ($len-2) &&
(ord($str[$i+1])&0x80) == 0x80 )
$i += 2;
else
return;
}
// 1Byte
else {
$i++;
}
}
}
function BEncode($str) {
$debug = $_POST[debug];
$i = 0;
$cut_len = 45;
$len = strlen($str);
$b64_str = "";
while( $i < $len ){
$tmp_str = substr( $str, $i , $cut_len );
utf8_validation( $tmp_str, &$end );
/* ignore broken utf8 character */
if( $end == 0 ){
$i++;
continue;
}
else if( $cut_len != $end )
$tmp_str = substr( $tmp_str, 0, $end );
if( $i )
$b64_str .= "\r\n";
$b64_str .= "=?utf-8?B?".base64_encode($tmp_str)."?=";
if( $debug ){
echo "1.original string: ". $tmp_str ."<br />";
echo "2.base64_encoded: ". base64_encode($tmp_str) ."<br />";
echo "3.length: ". $end."<br /><br />";
}
$i += $end;
}
return $b64_str;
}
if( $_POST[test] ){
echo "<tt>";
echo nl2br(BEncode($_POST[test]))."<br /><br /><br />";
echo "</tt>";
}
show_source(__FILE__);
?>
</body>
</html>