我需要在html中找到几个href,如下所示:
<table>
<tr><td><a href="url1">link1</a></td>
<td><a href="url2"><img src="image.jpg" /></a></td>
</tr>
</table>
一旦找到,我需要为每个href添加(替换)这样的东西:
?ID=1
因此html应该是这样的:
<table>
<tr><td><a href="url1?ID=1">link1</a></td>
<td><a href="url2?ID=1"><img src="image.jpg" /></a></td>
</tr>
</table>
任何帮助将不胜感激,谢谢,迈克。
答案 0 :(得分:0)
这是新代码:
Shared Function AdjustURL(ByVal psInput As String, ByVal psAppend As String) As String
Dim sOrig As String = " " & psInput 'Prepending a space (browser will ignore) in case the first character is a link.
Dim sOutput As String = ""
Dim iEnd As Integer = -1, iQuestion As Integer = -1
While sOrig.Length > 0
sOutput &= sOrig.Substring(0, 1)
sOrig = sOrig.Substring(1)
iEnd = -1
If sOrig.StartsWith("<a href=") Then 'Is this a safe assumption, what if it's not an <a> or href isn't next after only one space?
If sOrig.StartsWith("<a href=""") Then
iEnd = InStr(10, sOrig, """") 'URL ends with a Quote
Else
iEnd = InStr(9, sOrig, " ") 'URL ends with a Space - are there other possibilities?
End If
iQuestion = InStr(sOrig, "?")
If iQuestion > iEnd Then iQuestion = -1
sOutput &= sOrig.Substring(0, iEnd - 1) & IIf(iQuestion > -1, "?", "&") & psAppend
sOrig = sOrig.Substring(iEnd - 1)
End If
End While
Return sOutput
End Function
呼叫:
Dim sHTML As String = "<table> <tr><td><a href=""url1"">link1</a></td> <td><a href=""url2""><img src=""image.jpg"" /></a></td> </tr> </table>"
Response.Write(Class1.AdjustURL(sHTML, "ID=1"))
结果:
<table> <tr><td><a href="url1?ID=1">link1</a></td> <td><a href="url2?ID=1"><img src="image.jpg" /></a></td> </tr> </table>
我确信随着用户输入内容,您会发现其他替代品。我绝对建议用<script
替换<span style=display:none
以避免javascript注入。在将此代码写入数据库之前,您还需要确保避免使用SQL注入。