使用经典ASP替换HTML中的文本

3
我不确定这是否在经典的ASP(VBscript)中可能,但我想要做的是:

在HTML文本中搜索特定字符串(仅在p标签、div标签、li标签和span标签之间),并将其替换为其他内容。例如:

原始HTML

<p>Some text with the keyword.</p>

经典ASP函数

在所有p标签、div标签、span标签和li标签中,仅搜索HTML文本中的关键词,但忽略其他标签(如a标签)间的文本。将该关键词替换为其他内容。

输出HTML

<p>Some text with the <a href="someurl">keyword</a>.</p>

如果这不可能,还有其他的方法吗?有人有经验吗?
只是为了明确。 "Some text with the keyword." 的文本并不来自数据库,因为那样太容易了 :) 不幸的是,它在 .asp 文件的 HTML 中。

哇...好问题。您能否在同一个问题中添加PHP?我想用PHP来做。:) - Praveen Kumar Purushothaman
这相当复杂 - 你正在编写一个解析器。这对于一个StackOverflow问题来说有点过于宽泛了。 - Diodeus - James MacFarlane
你应该能够使用正则表达式来完成这个任务。 VBScript RegExp对象的参考和支持的语法在这里: http://www.regular-expressions.info/vbscript.html - theprogrammer
1个回答

0

这里需要进行字符串操作...由于我从来没有真正接触过正则表达式,所以很久以前我就用了一堆自己的方法来以各种方式操纵文本。相当丑陋,效率也不是最好的,但它能够工作。

首先,在您的代码中添加这些函数:

Function GetBetween(str, leftDelimeter, rightDelimeter)
    Dim tmpArr, result(), x
    tmpArr=Split(str, leftDelimeter)
    If UBound(tmpArr) < 1 Then
        GetBetween=Array() : Exit Function
    End If
    ReDim result(UBound(tmpArr)-1)
    For x=1 To UBound(tmpArr)
        result(x-1)=(Split(tmpArr(x), rightDelimeter))(0)
    Next
    Erase tmpArr
    GetBetween=result
End Function

Function ReplaceWholeWord(ByVal strText, strWord, strToReplaceWith)
    ReplaceWholeWord = strText
    If InStr(strText, strWord)<1 Then Exit Function

    'Text is only the word?
    If strText=strWord Then
        strText = Replace(strText, strWord, strToReplaceWith)
    Else  
        'Text starting with word?
        If InStr(strText, strWord & " ")=1 Then
            strText = strToReplaceWith & " " & Right(strText, Len(strText) - Len(strWord & " "))
        End If

        'Text ends with word?
        If Right(strText, Len(" " & strWord))=(" " & strWord) Then
            strText = Left(strText, Len(strText) - Len(" " & strWord)) & " " & strToReplaceWith
        End If

        'Text ends with word and a period?
        If Right(strText, Len(" " & strWord & "."))=(" " & strWord & ".") Then
            strText = Left(strText, Len(strText) - Len(" " & strWord & ".")) & " " & strToReplaceWith & "."
        End If

        'Replace between other words:
        strText = Replace(strText, " " & strWord & " ", " " & strToReplaceWith & " ")
    End If

    ReplaceWholeWord = strText
End Function

有了这些函数,下面是一个示例代码,可以按照您想要的方式替换关键字:

Const KEYWORD = "keyword"
Dim strHTML, arrTagsToFind, x
Dim curItemsArray,  y, curItem
Dim curReplacedItem
arrTagsToFind = Array("p", "div")
strHTML = "<b>this keyword will not be replaced</b><p>Some text with the keyword.</p>keyword here won't be replaced too<p>Some other text with the keyword22 that is not whole word but end with keyword</p><div>keyword first</div>"
For x=0 To UBound(arrTagsToFind)
    curItemsArray = GetBetween(strHTML, "<" + arrTagsToFind(x) + ">", "</" + arrTagsToFind(x) + ">")
    For y=0 To UBound(curItemsArray)
        curItem = curItemsArray(y)
        curReplacedItem = ReplaceWholeWord(curItem, KEYWORD, "<a href=""#foo"">" & KEYWORD & "</a>")
        strHTML = Replace(strHTML, curItem, curReplacedItem)
    Next
Next
Response.Write(strHTML)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接