今天突然想到之前有用户反应该内容关键字替换问题.描述如下:
假如你的文章内容如下:
KesionCMS是目前国内最强的<a href="http://www.kesion.com" title="CMS系统">asp</a>开发的CMS系统,她不仅使用简单,而且程序开源.<img src="http://www.kesion.com/images/logo.jpg" alt="KesionCMS标志" />....
并假设你在后台设置了内容关键字CMS,链接为http://www.kesion.com
那么这里生成的前台页面将会变成
Kesion<a href="http://www.kesion.com" class="innerlink">CMS</a>是目前国内最强的<a href="http://www.kesion.com" title="<a href="http://www.kesion.com" class="innerlink">CMS</a>系统">asp</a>开发的<a href="http://www.kesion.com" class="innerlink">CMS</a>系统,她不仅使用简单,而且程序开源.<img src=http://www.kesion.com/images/logo.jpg alt="Kesion<a href="http://www.kesion.com" class="innerlink">CMS</a>标志" />....
即会把所有cms都替换成 <a href="http://www.kesion.com" class="innerlink">CMS</a>
显然在title="CMS系统" alt="KesionCMS标志" 这两个地方是不应该替换的.否则页面将出现混乱.
出现这个情况是应该科汛系统的内容关键字替换直接使用replace替换所致.今天给大家讲的就是要改进这个替换的地方.不再直接使用replace函数.而是判断在><之间的关键字才给替换.修改代码如下
请用dw工具打开KS_Cls/Kesion.CommonCls.asp文件,并找到
'*************************************************************************************
'函数名:ReplaceInnerLink
'作 用:替换站内链接
'参 数:Content-待替换内容
'*************************************************************************************
Public Function ReplaceInnerLink(Content)
If Not IsObject(Application(SiteSN & "_InnerLink")) then
Dim Rs:Set Rs = Conn.Execute("Select Title,Url,OpenType,CaseTF,Times From KS_InnerLink Where OpenTF=1 Order By ID")
Set Application(SiteSN & "_InnerLink")=RecordsetToxml(Rs,"InnerLink","InnerLinkList")
Set Rs = Nothing
end if
Dim Node,CaseTF,Times
For Each Node In Application(SiteSN & "_InnerLink").DocumentElement.SelectNodes("InnerLink")
If InStr(Content,Node.selectSingleNode("@ks0").text)>0 Then
Dim OpenTypeStr:OpenTypeStr = G_O_T_S(Node.selectSingleNode("@ks2").text)
CaseTF=Cint(Node.selectSingleNode("@ks3").text)
Times=Cint(Node.selectSingleNode("@ks4").text)
Content= Replace(Content,Node.selectSingleNode("@ks0").text,"<a href="""&Node.selectSingleNode("@ks1").text&"""" & OpenTypeStr & " class=""innerlink"">"&Node.selectSingleNode("@ks0").text&"</a>",1,Times,CaseTF)
End if
Next
ReplaceInnerLink = HTMLCode(Content)
End Function
将上面这部分内容替换成以下代码,即可解决此问题
'*************************************************************************************
'函数名:ReplaceInnerLink
'作 用:替换站内链接
'参 数:Content-待替换内容
'*************************************************************************************
Public Function ReplaceInnerLink(Content)
Content=HTMLCode(Content)
If Not IsObject(Application(SiteSN & "_InnerLink")) then
Dim Rs:Set Rs = Conn.Execute("Select Title,Url,OpenType,CaseTF,Times From KS_InnerLink Where OpenTF=1 Order By ID")
Set Application(SiteSN & "_InnerLink")=RecordsetToxml(Rs,"InnerLink","InnerLinkList")
Set Rs = Nothing
end if
Dim Node,CaseTF,Times,Inti,DLocation,XLocation,StrReplace,CurrentTimes,SourceStr
For Each Node In Application(SiteSN & "_InnerLink").DocumentElement.SelectNodes("InnerLink")
Inti=1
CurrentTimes=0
Dim OpenTypeStr:OpenTypeStr = G_O_T_S(Node.selectSingleNode("@ks2").text)
CaseTF=Cint(Node.selectSingleNode("@ks3").text)
Times=Cint(Node.selectSingleNode("@ks4").text)
StrReplace=Node.selectSingleNode("@ks0").text
If InStr(1,Content,StrReplace,CaseTF)>0 Then
Do While instr(Inti,Content,StrReplace,CaseTF)<>0
Inti=instr(Inti,Content,StrReplace,CaseTF)
If Inti<>0 then
DLocation=instr(Inti,Content,">",CaseTF) '仅替换在><之间的关键字
XLocation=instr(Inti,Content,"<",CaseTF)
If DLocation >= XLocation Then
Content=left(Content,Inti-1) & "<a href="""&Node.selectSingleNode("@ks1").text&"""" & OpenTypeStr & " class=""innerlink"">"&Node.selectSingleNode("@ks0").text&"</a>" & mid(Content,Inti+len(StrReplace))
Inti=Inti+len("<a href="""&Node.selectSingleNode("@ks1").text&"""" & OpenTypeStr & " class=""innerlink"">"&StrReplace&"</a>")
CurrentTimes=CurrentTimes+1
If Times<>-1 And CurrentTimes>= Times Then Exit Do
Else
Inti=Inti+len(StrReplace)
End If
End If
Loop
End if
Next
ReplaceInnerLink = Content
End Function