分页: 1 / 1

需要html entity<->binary转换的。

发表于 : 2008-04-03 16:24
eexpress
&#x6587;&#x6863; 这样的是叫html entity吧,包括那些>的。不是的话,就改标题了。 :lol:
想要和utf8二进制互换的方法。

☎ '/usr/share/doc/libhtml-parser-perl/examples/htext' ~/.config/rox.sourceforge.net/ROX-Filer/Bookmarks.xml
似乎是可以?
反向不会。

xml -> utf8
$str =~ s/&#x([[:xdigit:]]+);/chr(hex($1))/ge;
utf8 -> xml 有些问题,思路正确。
$str =~ s/[\x80-]/sprintf("&#x%x;", ord $&)/g;
HTML::Entities or Encode::encode
是现成的,可有空再去看。

或者有其他直接的命令?系统带的?现成的?

--------------------------------
第2种情况:

%E5%AA%92%E4%BD%93 这样的,怎么搞。
$str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;

代码: 全选

☎  perl html2utf8.pl 
%E5%BA%94%E7%94%A8/%E8%84%9A%E6%9C%AC
应用/脚本
反向这样
$str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;

发表于 : 2008-04-03 16:25
yaoms
>属于 预定义实体

发表于 : 2008-04-03 16:26
zhan
urlencode 和 urldecode 什么的

自己去找脚本

发表于 : 2008-04-03 16:27
yaoms
第二种情况 python和php里都有函数实现

发表于 : 2008-04-03 16:28
eexpress
urlencode / urldecode in Perl

If you are looking for PHP's urlencode/urldecode equivalent in Perl, you won't find any. But here's what you can do:

Encode:
$str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;

Decode:
$str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;

测试

发表于 : 2008-04-12 20:24
hliang0813
如果是PHP中可以用mb_convert_encoding函数的

代码:
<?php
$str = "这是汉字";
$encoded_str = mb_convert_encoding($str, 'HTML-ENTITIES', "UTF-8");
echo $encoded_str;
?>