如何解决如何在 Tcl 中将 %hex 转换为字符?
我需要一些 TCL 方面的帮助。我以以下字符串为例:
%3Cdiv%20id%3D%22video-container-direct%22%20style%3D%22position%3A%20relative%3Bpadding-bottom%3A%2056.6%25%3Bpadding-top%3A%2090px
%3Bheight%3A%200%3Boverflow%3A%20hidden%3B%22%3E%3Ciframe%20allowfullscreen%3D%22%22%20frameborder%3D%220%22%20scrolling%3D%E2%80%9Cno
%22%20src%3D%22%2F%2Fwww.vsports.pt%2Fembd%2F68680%2Fm%2F8856%2Fmaisf%2Fce1702fde64b1cf799dded7f1f3ab428%3Fautostart%3Dfalse%22%20
style%3D%22position%3A%20absolute%3Btop%3A0%3Bleft%3A%200%3Bwidth%3A%20100%25%3Bheight%3A%20100%25%3Bborder%3A0%22%3E%3C%2Fiframe
%3E%3C%2Fdiv%3E
我想替换 % 之后的所有内容,当然要删除 % 。我的意思是,将 %2F
转为 /
,将 %3D
转为 =
,依此类推。
解决方法
简单的方法是使用 uri::urn 中的 tcllib 软件包(应该可以通过您操作系统的软件包管理器安装):
#!/usr/bin/env tclsh
package require uri::urn
set s "%3Cdiv%20id%3D%22video-container-direct%22%20style%3D%22position%3A%20relative%3Bpadding-bottom%3A%2056.6%25%3Bpadding-top%3A%2090px
%3Bheight%3A%200%3Boverflow%3A%20hidden%3B%22%3E%3Ciframe%20allowfullscreen%3D%22%22%20frameborder%3D%220%22%20scrolling%3D%E2%80%9Cno
%22%20src%3D%22%2F%2Fwww.vsports.pt%2Fembd%2F68680%2Fm%2F8856%2Fmaisf%2Fce1702fde64b1cf799dded7f1f3ab428%3Fautostart%3Dfalse%22%20
style%3D%22position%3A%20absolute%3Btop%3A0%3Bleft%3A%200%3Bwidth%3A%20100%25%3Bheight%3A%20100%25%3Bborder%3A0%22%3E%3C%2Fiframe
%3E%3C%2Fdiv%3E"
puts [uri::urn::unquote $s]
Tcl 8.7 将允许您使用 regsub
(URL 解码甚至是示例之一,复制如下):
# Match one of the sequences in a URL-encoded string that needs
# fixing,converting + to space and %XX to the right character
# (e.g.,%7e becomes ~)
set RE {(\+)|%([0-9A-Fa-f]{2})}
# Note that -command uses a command prefix,not a command name
set decoded [regsub -all -command $RE $string {apply {{- p h} {
# + is a special case; handle directly
if {$p eq "+"} {
return " "
}
# convert hex to a char
scan $h %x charNumber
format %c $charNumber
}}}]
puts $decoded
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。