开发者

Using SHIFT_JIS text in PHP

开发者 https://www.devze.com 2023-01-01 08:45 出处:网络
I am building a form that needs to accept characters encoded in SHIFT_JIS and开发者_如何学JAVA then send those results via email to a recipient. I\'ve tried to simply capture the results from the $_PO

I am building a form that needs to accept characters encoded in SHIFT_JIS and开发者_如何学JAVA then send those results via email to a recipient. I've tried to simply capture the results from the $_POST variable and then to insert them into a block of text like this:

$NameJp = $_POST['NameJp'];
$contents = <<<TEST

Name: $NameJp

...
TEST

but that doesn't appear to work (which doesn't really surprise me). This is my first attempt at dealing with non-ASCII characters in PHP and I am hoping that people might have some suggestions. Perhaps I am missing a simple function to encode the text.

Also, are there any other potential pitfalls that I might encounter?

Thanks.


Also, are there any other potential pitfalls that I might encounter?

Yes. :)

It's all fine and good to receive text in SHIFT_JIS, but you'll have to handle it like SHIFT_JIS all the way after that and inform everybody else that this text is in SHIFT_JIS. I.e., if you insert it into an email, you'll need to set appropriate email headers informing clients that this email contains text in SHIFT_JIS. If you need to display it on a web page or just in debug, you'll need to make sure the browser or debugging environment handles the text as SHIFT_JIS. If you mix it with other text, you should make sure they're in the same encoding.

Honestly, SHIFT_JIS is antiquated and terrible to work with. You should use UTF-8 if at all possible. If you absolutely, positively need to accept SHIFT_JIS and do a lot of work on it, you may want to convert it to UTF-8 internally and convert it back to whatever output encoding is necessary when the time comes. You can use iconv to do so.

Email headers are special BTW in that they can not contain anything but ASCII characters. The subject of an email is part of the header. To send non-ASCII characters as a subject line, you'll need to MIME encode it using mb_encode_mimeheader.

Also, the obligatory reference to: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号