2012年7月4日 星期三

分享 asp 副程式 檢查 字串是否為 html tag


'// purpose: 用 "本文內容" 自動判斷文章是 html or text, text 時把 enter 換 <br/>
'// ex: ret = htmlMessage(str)
function htmlMessage(byval str)
    str = trim("" & str)
   
    dim isHtmlContentFlag
    isHtmlContentFlag = IsHtmlContent(str)
    'response.write "<h1>isHtmlContentFlag:" & isHtmlContentFlag & "</h1>" & vbcrlf
   
    if isHtmlContentFlag then
        '// do nothing
    else
        '// do convert.
        str = replace(str,vbCRLF&vbCRLF,"<P>")
        str = replace(str,vbCRLF,"<BR>")
        str = replace(str,chr(10),"<BR>")
    end if
    htmlMessage = str
end function


'// purpose: 檢查 字串是否為 html tag.
'// PS: 其實也可以用 pattern 來做 match 應該會更好.
'// ex: ret = checkIsHtmlContent(str)
'// return:
'//     True: html
'//     False: text
function IsHtmlContent(byval str)
    dim isHtmlContentFlag
    isHtmlContentFlag = not True
   
    if instr(1, str,"<P ",1)>0 or instr(1, str,"<P>",1)>0  or instr(1, str,"<P/>",1)>0 then
        isHtmlContentFlag = True
    end if
    if instr(1, str,"<BR ",1)>0 or instr(1, str,"<BR>",1)>0 or instr(1, str,"<BR/",1)>0 then
        isHtmlContentFlag = True
    end if
    if instr(1, str,"<td",1)>0 and instr(1, str,"</td",1)>0 then
        isHtmlContentFlag = True
    end if
    if instr(1, str,"<ul",1)>0 and instr(1, str,"</ul",1)>0 then
        isHtmlContentFlag = True
    end if
    if instr(1, str,"<div",1)>0 and instr(1, str,"</div",1)>0 then
        isHtmlContentFlag = True
    end if
    IsHtmlContent = isHtmlContentFlag
end function

沒有留言:

張貼留言

Facebook 留言板