Home > programmazione > Programmare in ASP: Funzione per pulire testo da Word

Programmare in ASP: Funzione per pulire testo da Word

gennaio 31st, 2010 Invia Invia  Stampa Stampa

Ecco una funzione abbastanza complessa che serve per pulire una stringa passata, come input, in un’altra stringa di output priva della formattazione di Microsoft Word.

 

function CleanWord(stringa,bIgnoreFont,bRemoveStyles )

    stringa = replace_string(stringa,"/<o:p>\s*<\/o:p>/g","",True)
    stringa = replace_string(stringa,"/<o:p>.*?<\/o:p>/g","&nbsp;",True) 

    'Remove mso-xxx styles.
    stringa = replace_string(stringa,"/\s*mso-[^:]+:[^;""]+;?/gi","",True) 

    ' Remove margin styles.
    stringa = replace_string(stringa,"/\s*MARGIN: 0cm 0cm 0pt\s*;/gi","",True)
    stringa = replace_string(stringa,"/\s*MARGIN: 0cm 0cm 0pt\s*""/gi","",True)
    stringa = replace_string(stringa,"/\s*TEXT-INDENT: 0cm\s*;/gi","",True)
    stringa = replace_string(stringa,"/\s*TEXT-INDENT: 0cm\s*""/gi","",True)
    stringa = replace_string(stringa,"/\s*TEXT-ALIGN: [^\s;]+;?""/gi","",True)
    stringa = replace_string(stringa,"/\s*PAGE-BREAK-BEFORE: [^\s;]+;?""/gi","",True)
    stringa = replace_string(stringa,"/\s*FONT-VARIANT: [^\s;]+;?""/gi","",True)
    stringa = replace_string(stringa,"/\s*tab-stops:[^;""]*;?/gi","",True)
    stringa = replace_string(stringa,"/\s*tab-stops:[^""]*/gi","",True)

    ' Remove FONT face attributes.
    if ( bIgnoreFont ) then
        stringa = replace_string(stringa,"/\s*face=""[^""]*""/gi","",True)
        stringa = replace_string(stringa,"/\s*face=[^ >]*/gi","",True)
        stringa = replace_string(stringa,"/\s*FONT-FAMILY:[^;""]*;?/gi","",True)
    end if

    ' Remove Class attributes
    stringa = replace_string(stringa,"/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi","<$1$3",True)

    ' Remove styles.
    if ( bRemoveStyles ) then
        stringa = replace_string(stringa,"<(\w[^>]*) style=""([^\""]*)""([^>]*)/gi","<$1$3",True)
        ' Remove empty styles.
        stringa = replace_string(stringa,"/\s*style=""\s*""/gi","<$1$3",True)
        stringa = replace_string(stringa,"/<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi","&nbsp;",True)
        stringa = replace_string(stringa,"/<SPAN\s*[^>]*><\/SPAN>/gi","",True)
        stringa = replace_string(stringa,"/<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi","<$1$3",True)

        ' Remove Lang attributes
        stringa = replace_string(stringa,"/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi","<$1$3",True)
        stringa = replace_string(stringa,"/<SPAN\s*>(.*?)<\/SPAN>/gi","$1",True)
        stringa = replace_string(stringa,"/<FONT\s*>(.*?)<\/FONT>/gi","$1",True)

        ' Remove XML elements and declarations     
        stringa = replace_string(stringa,"/<\\?\?xml[^>]*>/gi","",True)
        ' Remove Tags with XML namespace declarations: <o:p><\/o:p>
        stringa = replace_string(stringa,"/<\/?\w+:[^>]*>/gi","",True)
        ' Remove comments [SF BUG-1481861].
        stringa = replace_string(stringa,"/<\!--.*?-->/g","",True)
        stringa = replace_string(stringa,"/<(U|I|STRIKE)>&nbsp;<\/\1>/g","&nbsp;",True)
        stringa = replace_string(stringa,"/<H\d>\s*<\/H\d>/gi","",True)

        ' Remove "display:none" tags.
        stringa = replace_string(stringa,"/<(\w+)[^>]*\sstyle=""[^""]*DISPLAY\s?:\s?none(.*?)<\/\1>/ig","",True)
        ' Remove language tags
        stringa = replace_string(stringa,"/<(\w[^>]*) language=([^ |>]*)([^>]*)/gi","<$1$3",True)
        ' Remove onmouseover and onmouseout events (from MS Word comments effect)
        stringa = replace_string(stringa,"/<(\w[^>]*) onmouseover=""([^\""]*)""([^>]*)/gi","<$1$3",True)
        stringa = replace_string(stringa,"/<(\w[^>]*) onmouseout=""([^\""]*)""([^>]*)/gi","<$1$3",True)
    end if
    stringa = replace_string(stringa,"/<(\w[^>]*) onmouseout=""([^\""]*)""([^>]*)/gi","<$1$3",True)

    ' Remove empty tags (three times, just to be sure).
    ' This also removes any empty anchor

    stringa = replace_string(stringa,"/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g","",True)
    stringa = replace_string(stringa,"/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g","",True)
    stringa = replace_string(stringa,"/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g","",True)
    CleanWord = stringa
end function

Articoli correlati


VEDI ANCHE:

  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.