2012-11-13 85 views
3

替換HTML文本,我不知道這是即使在傳統的ASP(VBScript)的,但我想要做的事情可能是這樣的:使用傳統的ASP

搜索在HTML文本的特定字符串(只在p-tags,div-tags,li-tags,span-tags之間以及忽略其他標籤(如a-tags)之間的文本)並用其他內容替換。例如:

原始的HTML

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

經典ASP功能

搜索僅在所有P標籤之間的HTML文本關鍵字,DIV標籤,跨越標籤和li標籤,但忽略其他標籤之間的文本,如標籤。用其他的替換關鍵字。

輸出HTML

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

如果這是可能,是否有任何其他方式做到這一點? 任何有此經驗的人?

只是要清楚。 「有關鍵字的一些文本」 - 文本不是來自數據庫,因爲這太容易了:)不幸的是,它存在於.asp文件的HTML中。

+0

哇...尼斯問題。你可以在同一個問題中添加PHP嗎?想在PHP中做。 ':)' –

+0

這非常複雜 - 你正在編寫一個解析器。這對StackOverflow問題有點過分。 –

+0

你應該可以使用正則表達式來做到這一點。 VBScript RegExp對象的引用和支持的語法如下: http://www.regular-expressions.info/vbscript.html – theprogrammer

回答

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)