0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UTF8 と Shift_JIS による改行の挙動の誤認されやすい要素 perl

Posted at

簡単に改行をまとめると。

use Encode;

# SJISの器をUTF-8に変換
my $sjis_value = ...;  # SJISで受け取った文字列
my $value = decode('Shift_JIS', $sjis_value);

# 改行処理(UTF-8なら安全)
$value =~ s/\r\n/<br>/g;  # Windows は 2文字のゴードに確約されている。
$value =~ s/\n/<br>/g;    # 書式的に 1Byteコードである\nは文字ではなく制御コードである。

# Sift_jis の場合 2Byteコードである。
# $value =~ s/\r\n/<br>/g;  # Windows は 2文字のゴードに確約されている。
# $value =~ s/\n/<br>/g;    # 一見正しそうだが文字にも含まれている場合がある。
$value =~ s/\n\n/<br>/g;    # 結果的にLine end は\n\nになっている
# $value =~ s/\n\n\n\n/EOF/g;    # フィールドエンド的に使うことがある。4つで1つとなる。

# 必要なら再エンコード
print encode('UTF-8', $value);

よく LINAX は\nだけで終わっていそうに見えている。

Linux系で \n だけでセパレートするとくるうことがある。

匿名

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?