Project1
标题:
字符串编码 UTF8 转 URL
[打印本页]
作者:
后知后觉
时间:
2013-3-8 15:16
标题:
字符串编码 UTF8 转 URL
虽然这个帖子的标签是[通用发布]
不过我并没有在 XP 和 VX 上测试~
这个东西的主要用途大概就是这个了~
http://rpg.blue/forum.php?mod=viewthread&tid=176116
这个转换函数是根据其他语言改的.
编码这东西我也不清楚.如果出现BUG.我无法解决.请慎用~
http://myvs01.blog.163.com/blog/static/1853340902012524114731486/
下面这是在 RM 里的定义
#==============================================================================
# ■ 本代码来自 www.66rpg.com 使用或转载请保留此信息
#------------------------------------------------------------------------------
# 功能:将UTF8字符串转换为URL编码
# 作者:后知后觉(66rpg.com)
# 说明:
# 本代码由参考其他语言的代码转换而来
# 转换源地址:
# http://myvs01.blog.163.com/blog/static/1853340902012524114731486/
# 使用方法:
# 对字符串调用 to_url 即可获得 URL编码 的字符串新实例
#==============================================================================
class String
def to_url
strDest = ""
hzhj = 0
each_byte do |byte|
if hzhj > 0
strDest += sprintf("%%%02X", byte)
hzhj -= 1
elsif byte <= 0x7f
bytechr = byte.chr
# 字母和数字不转换
if (bytechr >= '0' && bytechr <= '9') ||
(bytechr >= 'A' && bytechr <= 'Z') ||
(bytechr >= 'a' && bytechr <= 'z')
strDest += bytechr
elsif bytechr == ' '
# 空格转换成 + 号
strDest += '+'
else
# 其他标点符号
strDest += sprintf("%%%02X", byte)
end
else
# 汉字或者其他的uft8文字,每3个字节一转
strDest += sprintf("%%%02X", byte)
hzhj = 2
end
end
strDest
end
end
复制代码
下面这是一段用来测试的代码.
module Hzhj; end
module Hzhj::SNS
module_function
def init_sns
title = ("UTF8ToURL编码函数测试2 这次测试点符号什么的《|,.<>=+-*/~!@\#$%^&*()_》[]{};\':\"/?").to_url
url = "http://www.66rpg.com/"
pic = ""
@sns = {}
@sns[:xlwb] = ["新浪微博",
"http://v.t.sina.com.cn/share/share.php?pic=#{pic}&title=#{title}"]
@sns[:txwb] = ["腾讯微博",
"http://v.t.qq.com/share/share.php?title=#{title}&url=#{url}&pic=#{pic}"]
@sns[:wywb] = ["网易微博",
"http://t.163.com/article/user/checkLogin.do?&info=#{title} #{url}"]
@sns[:qqkj] = ["QQ空间",
"http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?title=#{title}&url=#{url}"]
@sns[:bdkj] = ["百度空间",
"http://apps.hi.baidu.com/share/?title=#{title}&url=#{url}"]
@sns[:rrw] = ["人人网",
"http://share.renren.com/share/buttonshare.do?link=#{url}"]
title = ("RGSS UTF8 String To URL Coding").to_url
txt = ("RGSS UTF8 String To URL Coding").to_url
@sns[:kxw] = ["开心网",
"http://www.kaixin001.com/~repaste/share.php?rtitle=#{title}&rurl=#{url}&rcontent=#{txt}"]
end
def call(url)
bb_66rpg = Win32API.new('shell32.dll', 'ShellExecuteA', 'pppppi', 'i')
bb_66rpg.call(0, 'open', url, 0, 0, 1)
end
def sns
@sns
end
def name(symbol)
@sns[symbol] ? @sns[symbol][0] : "???"
end
def share(symbol)
call(@sns[symbol][1]) if @sns[symbol]
end
end
Hzhj::SNS.init_sns
Hzhj::SNS.share(:xlwb)
Hzhj::SNS.share(:txwb)
Hzhj::SNS.share(:wywb)
Hzhj::SNS.share(:qqkj)
Hzhj::SNS.share(:bdkj)
Hzhj::SNS.share(:rrw)
Hzhj::SNS.share(:kxw)
复制代码
大概就是这样了~
分享按钮什么的请自行解决~
作者:
aa479094467
时间:
2013-3-9 13:13
呃,感觉还不错,让我的脑海里又多了一条思路
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1