使用vbscript的字符串操作函数处理文本 |
作者:未知 文章来源:未知 点击数:255 更新时间:2007-8-16 |
我们可以使用vbscript或者asp支持的其他脚本在服务器端对获取的信息进行整理。这些函数都是很常见的,现在把它们拿出来,好像也没有什么用。 1、instr函数 Instr() FUNCTION: 返回字符或字符串在另一个字符串中第一次出现的位置. SYNTAX: Instr([start, > strToBeSearched, strSearchFor [, compare>) ARGUMENTS: Start为搜索的起始值,strToBeSearched接受搜索的字符串 strSearchFor要搜索的字符compare 比较方式(详细见ASP常数) EXAMPLE: <% strText = "This is a test!!" pos = Instr(strText, "a") response.write pos %> RESULT: 9 2、instrrev函数 InstrRev() FUNCTION: 同上,只是从字符串的最后一个搜索起 SYNTAX: InstrRev([start, > strToBeSearched, strSearchFor [, compare>) ARGUMENTS: 同上. EXAMPLE: <% strText = "This is a test!!" pos = InstrRev(strText, "s") response.write pos %> RESULT: 13 3、mid函数 Mid() FUNCTION: 返回特定长度的字符串(从start开始,长度为length). SYNTAX: Mid(string, start [, length>) ARGUMENTS: EXAMPLE: <% strTest = "This is a test! Today is Monday." response.write Mid(strTest, 17, 5) %> RESULT: Today 4、replace Replace() FUNCTION: Returns a string in which a specified sub- string has been replaced with another substring a specified number of times. SYNTAX: Replace(strToBeSearched, strSearchFor, strReplaceWith [, start [, count [, compare>>>) ARGUMENTS: strToBeSearched is a string expression containing a sub-string to be replaced; strSearchFor is the string expression to search for within strToBeSearched; strReplaceWith is the string expression to replace sub-string strSearchFor; start (optional) is the numeric character position to begin search; count (optional) is a value indicating the comparision constant. EXAMPLE: <% strTest = "This is an apple!" response.write Replace(strTest, "apple", "orange") %> RESULT: This is an orange! 以上的四个函数是在远程获取中用的最多的,比起正则表达式,虽然功能上不足,但是使用他们显得更加简易。 另外一些可能用到的 trim() FUNCTION: 去掉字符串两边的空格. SYNTAX: Rtrim(string) ARGUMENTS: EXAMPLE: <% strTest = " This is a test!! " response.write RTrim(strTest) %> RESULT: This is a test!! 还有ltrim和rtrim,分别是去掉字符串左边和右边的空格。StrReverse() FUNCTION: 反排一字符串 SYNTAX: StrReverse(string) ARGUMENTS: EXAMPLE: <% strTest = "This is a test!!" response.write StrReverse(strTest) %> RESULT: !!tset a si sihT
例子是落伍者论坛 mofei的文章。 <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'> <meta name='generator' content='默飞冲天'> <meta name='keywords' content='默飞冲天'> <style type=text/css>body {font-family: Arial; font-size: 12px;color: #000000;}td {font-family: Arial; font-size: 12px;line-height:20px;}a:link,a:visited{text-decoration: none; color: #000000}a:hover { color: #ff0000; text-decoration: underline}</style> <title>天气预报 - 默飞冲天</title> </head> <body> <% '制作:默飞 'QQ:33224360 'http://mofei.xinxiu.com '天气预报最新适用采集程序 '声明取得目标信息的函数,通过XML组件进行实现。 Function GetURL(url) Set Retrieval = CreateObject("Microsoft.XMLHTTP") With Retrieval .Open "GET", url, False .Send GetURL = bytes2bstr(.responsebody) '对取得信息进行验证,如果信息长度小于100则说明截取失败 if len(.responsebody)<100 then response.write "获取远程文件 <a href="&url&" target=_blank>"&url&"</a> 失败。" response.end end if End With Set Retrieval = Nothing End Function ' 二进制转字符串,否则会出现乱码的! function bytes2bstr(vin) strreturn = "" for i = 1 to lenb(vin) thischarcode = ascb(midb(vin,i,1)) if thischarcode < &h80 then strreturn = strreturn & chr(thischarcode) else nextcharcode = ascb(midb(vin,i+1,1)) strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode)) i = i + 1 end if next bytes2bstr = strreturn end function '声明截取的格式,从Start开始截取,到Last为结束 Function GetKey(HTML,Start,Last) filearray=split(HTML,Start) filearray2=split(filearray(1),Last) GetKey=filearray2(0) End Function Dim Softid,Url,Html,Title '把下面的[将乐]改成你的城市,就是你城市的天气预报了,可到www.tq121.com.cn里查一下,有没收录你的城市,一般到县级的都有。 Url="http://weather.tq121.com.cn/detail.php?city=将乐" Html = GetURL(Url) '搜索要偷取的内容的开始位置 Str_start = instr(Html, "预报发布时间") Str_start = instr(Str_start, Html, "<table width=""979"" height=""185"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"">") '搜索要偷取的内容的结束位置 Str_end = instr(Str_start, Html, "<table width=""979"" height=""105"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"">") '截取内容 Html = Mid(Html, Str_start, Str_end - Str_start) '调整表格 Html=replace(replace(Html,"width=""979""","width=100%"),"height=""185""","") '大表格 Html=replace(replace(Html,"width=""133""","width=95%"),"height=""183""","") '背景大小 Html=replace(replace(Html,"width=""973""",""),"height=""183""","") Html=replace(replace(Html,"width=""113""","width=100%"),"height=""35""","") '日期表格 Html=replace(replace(Html,"width=""57""",""),"width=""56""","") '图片表格 Html=replace(replace(Html,"width=""50""",""),"height=""46""","") Html=replace(replace(Html,"height=""57""",""),"height=""73""","") Html=replace(Html,"width=""141""","width=14%") '表格分配 Html=replace(Html,"width=""140""","width=14%") Html=replace(Html,"width=""139""","width=14%") Html=replace(Html,"width=""133""","width=16%") Html=replace(Html,"width=""125""","width=100%") Html=replace(Html,"width=""107""","width=100%") Html=replace(replace(Html,"../images/","images/"),"./images/","images/") '图片路径 response.write Html 'Response.Write"采集完毕!" %> </body> </html> |