返回列表 发帖

[文本处理] [已解决]批处理怎样把文本的字符串编码化urlencode?

本帖最后由 w1983912 于 2011-6-1 18:03 编辑

我也不知道该怎么表达这个需求 。。。。。。

a.txt内容:
xmyyy2y@@知道
xx55lud@@合同x路
5abccc@@人民
pxeke5xxx@@旅游abcd


如 第一行        xmyyy2y@@知道          得到      <a href="/x/xmyyy2y/%e7%9f%a5%e9%81%93.html">知道</a>
                     xx55lud@@合同x路       得到      <a href="/x/xx55lud/%e5%90%88%e5%90%8cx%e8%b7%af.html">合同x路</a>

注意: "知道"的utf-8格式的url编码(urlencode)是"%e7%9f%a5%e9%81%93"        参考转换:http://tool.chinaz.com/Tools/URLEncode.aspx
         "合同x路"的utf-8格式的url编码(urlencode)是"%e5%90%88%e5%90%8cx%e8%b7%af"

以上只是把字符串变成utf-8的编码   希望不会搞混   同样也需要字符串变成gbk的编码的批处理版本

以上只列出几行  a.txt 是gbk格式的文本 得到的x.txt也是gbk格式 有N行  


    请知道的朋友尽量帮帮忙 谢谢了
1

评分人数

    • zm900612: 感谢给帖子标题标注[已解决]字样PB + 2

你PHP不是很牛么,怎么不用PHP

TOP

本帖最后由 wankoilz 于 2011-6-1 11:09 编辑

我这有个vbs,你参考下:
Function URLEncoding(vstrIn)
strReturn = ""
For i = 1 To Len(vstrIn)
    ThisChr = Mid(vStrIn,i,1)
    If Abs(Asc(ThisChr)) < &HFF Then
        strReturn = strReturn & ThisChr
    Else
        innerCode = Asc(ThisChr)
        If innerCode < 0 Then
            innerCode = innerCode + &H10000
        End If
        Hight8 = left(cstr(hex(innerCode)),2)
        Low8 = right(cstr(hex(innerCode)),2)
        strReturn = strReturn & "%" & Hight8 & "%" & Low8
    End If
Next
URLEncoding = strReturn
End Function
Function URLUncoding(code)
str="0123456789abcdef"
set reg=new regexp
reg.ignoreCase=true
reg.global=true
reg.pattern="%\w\w%\w\w"
set matches=reg.execute(code)
for each matche in matches
    matche=replace(matche,"%","")
    sum=0
    for i=1 to 4
        singlechr=mid(matche,i,1)
        number=instr(1,str,Lcase(singlechr),1)-1
        sum=sum+number*16^(4-i)
    next
    word=chr(sum-65536)
    URLUncoding=URLUncoding&word
next
End Function
s=inputbox("输入Code或中文")
If inStr(s,"%") then
    return=msgbox(URLUncoding(s),0,"结果")
Else
    return=msgbox(URLEncoding(s),0,"结果")
End ifCOPY
抱歉,刚才仓促回帖,没注意到是utf-8,这个vbs只支持gbk

TOP

批处理如何把中文转为URL编码?
http://bbs.bathome.net/thread-6084-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

谢谢以上朋友的指点 代码过于烦琐 我这菜鸟级的 很难凑出来 很费精力  现在也只能一个文件夹一个文件夹的弄了

TOP

PHP水平有待提高
<?php
$in = fopen('a.txt', 'rb');
$out = fopen('x.txt', 'wb');
if (!$in || !$out) die();
while (!feof($in)) {
$str = trim(fgets($in));
$str = iconv('gbk', 'utf-8', $str);
$arr = explode('@@', $str);
$str = sprintf('<a href="/x/%s/%s">%s</a>',
urlencode($arr[0]), urlencode($arr[1]), $arr[1]);
$str = iconv('utf-8', 'gbk', $str);
fputs($out, "$str\r\n");
}
fclose($in);
fclose($out);
?>COPY

TOP

感谢
说实话,不怕大家笑话  我根本就没学任何语言  都是需要用到才学一下  功能我知道 但是不会写代码  只会稍微修改和照搬代码  不会灵活使用  想法虽有 就是不会代码  郁闷啊
能得到大家的帮助 很高兴 这也是我继续做站的动力  光搜索到的还是有很多不足

TOP

5# w1983912


用for命令遍历一下文件夹即可。

批处理for命令从入门到精通以及配套练习题
http://bbs.bathome.net/thread-2189-1-1.html
http://bbs.bathome.net/thread-2336-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表