如何可以从左到右匹配一部分?

作者站长头像
站长
· 阅读数 5

下面是乔治老师的代码,一切正常。最大的优点是可以从左到右匹配一部分。比如:aa = "11.9.67.180"ipList 里面是 11.9.这样也能匹配到。我要的正是这样的效果。

<%
Dim aa, ipList, ip, found
aa = "11.9.67.180"
ipList = Array("99.88", "110.52", "43.80.235", "11.9.")
found = False

For Each ip In ipList
    If InStr(aa, ip) > 0 Then
        found = True
        Exit For
    End If
Next

If found Then
    Response.Write "ok"
    Response.End
End If
%>

但是改成如下后,就只能完全匹配了。我希望像上面那样可以从左到右匹配一部分。请问如何修改?谢谢。

<%
Dim aa, ipList, ip, found, dbIPList
aa = Request.ServerVariables("REMOTE_ADDR")

dbIPList = Rs("ip")

ipList = Split(dbIPList, ",")

found = False

For Each ip In ipList
    ip = Trim(ip)
    If InStr(aa, ip) > 0 Then
        found = True
        Exit For
    End If
Next

If found Then
    Response.Write "ok"
    Response.End
End If
%>

数据库的内容是这样的:99.88, 110.52, 43.80.235, 11.9.67.180

也可以是这样的:"99.88", "110.52", "43.80.235", "11.9.67.180"

只要哪种能用就可以。谢谢。

回复
1个回答
avatar
test
2024-07-09

要同时处理下面两种格式的数据库内容看这个修改: 99.88, 110.52, 43.80.235, 11.9.67.180"99.88", "110.52", "43.80.235", "11.9.67.180"

<%
Dim aa, ipList, ip, found, dbIPList
aa = Request.ServerVariables("REMOTE_ADDR")

dbIPList = Rs("ip")

' 增加一个去除双引号的操作,确保两种数据库内容格式都能正常处理
dbIPList = Replace(dbIPList, """", "")

ipList = Split(dbIPList, ",")

found = False

For Each ip In ipList
ip = Trim(ip)
If InStr(aa, ip) > 0 Then
found = True
Exit For
End If
Next

If found Then
Response.Write "ok"
Response.End
End If
%>
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容