A new writer on the list? I think so!

On Thu, Feb 3, 2011 at 8:36 AM, Shlomit Afgin
<shlomit.af...@weizmann.ac.il>wrote:

>
>
> Hello,
>

Hey Shlomit! :)


>
>
> I tried to convert html special characters to their real character.
>
> For example, converting    &#8221;      to     "   .
>
>
>
> I had the string
>
> $str = "&#8220; test &#8221; ניסיון ";
>
> The string contain also Hebrew letters.
>
>
>
> Any ideas are welcome!!
>

Seems like you worked hard on this. The solution is actually pretty simple
and it's what Omer and Shlomi suggested.
Your first attempt (using decode_entities) is correct, but you need for
first decode the Hebrew.

Here is an example that will work:
use strict;
use warnings;
use HTML::Entities;
use Encode 'decode';

sub get_string { ... }
my $string = get_string();
my $converted = decode_entities( decode( 'UTF-8', $string ) );
---

Of course, if you're defining the string hardcoded in your script, you need
to tell the interpreter that you're doing it by using the "utf8" pragma to
say "this file contains utf8 chars":
use strict;
use warnings;
use utf8;
use HTML::Entities;
use Encode 'decode';

my $string = "&#8220; test &#8221; ניסיון ";
my $converted = decode_entities( decode( 'UTF-8', $string ) );
---

And one more example. If you're going to print this string to a terminal,
you need to make sure that STDOUT (which is what you're actually printing
to) is in binmode, so it can show Hebrew properly:
use strict;
use warnings;
use utf8;
use HTML::Entities;

my $string = "&#8220; test &#8221; ניסיון ";
my $converted = decode_entities($string);
binmode STDOUT, ':utf8';

print "$converted\n";
---

You'll notice in the last example, that I'm not using Encode's decode()
function. That is because I'm declaring the file to have UTF8 characters,
which means the Hebrew that is hardcoded in the file is now considered UTF-8
anyway, and does not need to be decoded.

Hope that helps! :)

Sawyer.
_______________________________________________
Perl mailing list
Perl@perl.org.il
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to