Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PERL: how to detect string encoding so I can use the right charset

I have these 2 example strings:

$a = "點看"; 
$b = "pøp";

First one is displayed correctly using charset UTF-8, but second string not. Second is displayed correctly if charset is changed to iso-8859-1.

I don't know how to display latin1 characters with charset utf-8. Or at least, I need a solution to detect string type (e.g this is "utf-8" or this is "iso-8859-1"), so I can use appropriate charset to display it.

like image 823
Claude Avatar asked Dec 18 '25 13:12

Claude


1 Answers

Decode inputs. Encode outputs.

use strict;
use warnings qw( all );
use feature qw( say );

use utf8;                             # Source code is encoded using UTF-8
use open ':std', ':encoding(UTF-8)';  # Terminal expects UTF-8

my $s1 = "點看"; 
my $s2 = "pøp";

say for $s1, $s2;
like image 117
ikegami Avatar answered Dec 20 '25 16:12

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!